윈도우 앱개발을 향하여

블로그 이미지
윈도우 10 스토어에 앱을 개발해 올리는 것을 목표로 하고 있습니다. 비전공자가 독학으로 시도하는 일이어서 얼마나 걸릴지 모르겠지만... 아무튼 목표는 그렇습니다!!
by 코딩하는 경제학도
  • Total hit
  • Today hit
  • Yesterday hit

Copyright

이 모든 내용은 Pluralsight에 Xavier Morera가 올린 'Getting Started with JSON in C# Using Json.NET'라는 강의의 다섯번째 챕터를 듣고 정리한 것입니다(https://app.pluralsight.com/library/courses/json-csharp-jsondotnet-getting-started/table-of-contents).


Content

Serialization Fundamentals

Settings & Attributes

Custom Serialization

Performance Tips

LINQ to JSON

JSON & XML

Binary JSON (BSON)

Json.NET Schema


Outline

Manual Serialization/Deserialization

Fragments

Populate Objects

Merge Array Handling

Attributes

Memory Usage



Serializing and deserializing manually

Json.NET is extremely fast but it relies on reflection so if speed is key, serialize/deserialize manually using JsonTextReader/Writer. (Avoids using Reflection, Less memory usage, Fastest way of reading and writing JSON)


Writing

var builder = new StringBuilder(); //Never to use strings(immutable) if you're going to be changing them too much

StringWriter writer = new StringWriter(builder);

using (var jsonWriter = new JsonWriter(writer))

{

jsonWriter.Formatting = Formatting.Indented;

jsonWriter.WriteStartArray();

for (int i =0; i < numberOfView; i++)

{

jsonWriter.WriteStartObject();

....

jsonWriter.WriteEndObject();

}

jsonWriter.WriteEndArray();

}


Reading

var reader = new JsonTextReader(new StringReader()jsonStrings);

while(reader.Read())

{

if(reader.Value != null)

{

if(reader.TokenType == JsonToken.String....)

}

}



JSON Fragments (Deserialize only what you need!)

Large JSON documents or objects may take a lot of time serializing and deserializing. However, in certain scenarios you may have a very big JSON object, but you're only interested in a specific subsection of your data. With Json.NET it is possible to extract and work with a fragment of a JSON object using Linq.


Json.NET has the capability of extracting a subsection of a big JSON text, allowing you to deserialize only that small section. This is very beneficial both in terms of performance and simplicity because you're not deserializing the entire object, that's the first one, and second, it makes the code much more readable.


List<UserInteraction> userLogs = GetTestData(); //Generate huge data

string bigLog = JsonConvert.SerializeObject(userLogs);


JArray logs = JArray.Parse(bigLog); //bigLog is huge json string

List<CourseView> courses = new List<CourseView>(); //CourseView is an objects which we are interest in bigLog 

foreach (JObject logEntry in logs)

{

courses.Add(logEntry["courseView"].ToObject<CourseView>()); //Using Linq to json to extract CourseView

}


Remember that readable and simple code is much better than code that's complex and difficult to read. By using JSON fragments it makes your code much more readable and easier to maintain.



PopulateObject

With JSON fragments you're able to extract and work with sections of a large JSON object. PopulateObject is the opposite functionality where you're able to write specific values to a large JSON object.


List<UserInteraction> userLogs = GetTestData(); //Generate huge data


string jsonReviewed = "{'reviewed' : true. 'processedBy' : [''ReviewerProcess'], 'reviewedDate : '" + DateTime.Now.ToUniversalTime().ToString("yyyy-mm-ddTHH:mm:ssK") + @"' }";


foreach(UserInteraction log in userLogs)

{

JsonConvert.PopulateObject(jsonReviewed, log);

}



JSON Merge (Merge JSON Objects) (Skip)

There are cases where you have two JSON objects that you need to merge. For example, we might have a large JSON array where each item might need to read an object from a different data source. This can be a complex operation or not. Json.NET provides the merge functionality from one object to another. Logic is very simple. Name value pairs are copied across, skipping nulls if the existing property is not null and you can even specify how to handle arrays via merge array handling. You have four options for arrays, concat, union, replace, and merge.



Attributes for performance (Serialize and Deserialize Only What You Need) (Skip)

JsonIgnore Attributes on class



Optimizing Memory Usage (Avoid Large Object Heap, Use Streams)

Memory usage is critical to performance, but more than that, it can lead to exceptions. Any object over 85 kilobytes in size goes directly to a large object heap and this can mean a few out of memory exception. This number(85) is a threshold that was obtained after multiple tests by Microsoft. The problem with the large object heap as opposed to heap generations 0, 1, and 2, is that it's not compacted and even though there are some improvements in .NET Framework 4.5 and up, the promise that your memory usage may grow and grow until your application can be in trouble. Json.NET helps you improve memory usage by using streams instead of loading entire strings into memory. It achieves this by reading from a client in a synchronous way, reading one piece at a time a large JSON.


using (StreamReader streamReader = new StreamReader(stream))

{

using (JsonReader jsonReader = new JsonTextReader(streamReader))

{

var jsonSerializer = new JsonSerializer();

List<UserInteraction> logsStream = jsonSerializer.Deserialize<List<UserInteraction>>(jsonReader);


//The key here is that we're loading the text in an asynchronous way

}

}


출처

이 모든 내용은 Pluralsight에 Xavier Morera가 올린 'Getting Started with JSON in C# Using Json.NET'라는 강의의 다섯번째 챕터를 듣고 정리한 것입니다(https://app.pluralsight.com/library/courses/json-csharp-jsondotnet-getting-started/table-of-contents). 제가 정리한 것보다 더 많은 내용과 Demo를 포함하고 있으며 최종 Summary는 생략하겠습니다. Microsoft 지원을 통해 한달간 무료로 Pluralsight의 강의를 들으실 수도 있습니다.

'Programming > etc' 카테고리의 다른 글

(Json.NET) Custom Serialization  (0) 2018.03.30
(Json.NET) Settings & Attributes  (0) 2018.03.30
(Json.NET) Serialization Fundamentals  (0) 2018.03.30
AND

ARTICLE CATEGORY

분류 전체보기 (56)
Programming (45)
MSDN (4)
개발노트 (2)
reference (5)

RECENT ARTICLE

RECENT COMMENT

CALENDAR

«   2024/04   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30

ARCHIVE