윈도우 앱개발을 향하여

블로그 이미지
윈도우 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

Serialization And Deserialization

JsonSerializer and JsonConvert

JsonTextReader and JsonTextWriter

Dates in JSON

Error Handling


Serialization And Deserialization

Serialization and deserialization is the main functionality of Json.NET. Serialization and deserialization involves taking a data structure or object and converting it back and forth between JSON text and .NET objects. A .NET object when serialized can be stored as a stream of bytes of file or in memory and later it can be used to recreate the original object. You have to be careful though. In some cases there are private implementation details that are not available when deserializing or serializing, so you need to review the recreated object to determine if there is any information missing. In the serialization and deserialization process, you map property names and copy their values using the main JSON serializer class with the support of JsonReader and JsonWriter.



JsonSerializer and JsonConvert

The JsonSerializer class is a straightforward way of converting between JSON text and .NET objects. It provides a great deal of control and customization, being able to read and write directly to streams via JSON text reader and JSON text writer.

Simply use the serialize and deserialize methods.


JsonSerializer serializer = new JsonSerializer();

serializer.Serialize()...


And it gets even better. Json.NET comes with a very easy to use wrapper over JsonSerializer called JsonConvert that makes the serialization process that easy for most scenarios.

Simply use the SerializeObject and DeserializeObject methods.


string author = JsonConvert.SerializeObject(AuthorObject);

DeserializeObject<Author>(author);


Caution : There are many classes in Json.NET that provide async methods. Stay away from them as they are now marked as obsolete.



Demo note

1. Formatting.Indented setting on JsonConvert.SerializeObject method

2. PreserveReferenceHandling.Objects setting on JsonConvert.SerializeObject method


Whenever you're using Json.NET to serialize a class, by default it will serialize the object by value. This means that is if you have a class that has two references to another class, it will basically create two copies of the values. This might not be a problem if you're just serializing the class, but what happens if you want to deserialize, this is converted back again from JSON text to a .NET object and you want to have two references to the same object instead of two different objects and this is where preserving object references comes into play.


자기자신을 포함하는 리스트를 갖는 class를 Serialize할 때

PreserveReferenceHandling.Objects(want to preserve object) setting on JsonConvert.SerializeObject method

==> preserve reference to the objects, so when deserializing, Deserializer will not create duplicate classes.


3. Dynamic, ExpandoObject

dynamic varName = new ExpandoObejct(); //ExpandoObject class == members can be Dynamically added and removed at runtime.

... add members to varName...

then just pass varName to JsonConvert.SerializeObject method, and Deserialize it with dynamic


4. JsonConvert.DesrializeObject<Dictionary<string, string>>(...)


5. AnonymousType

JsonConvert.DeserializeAnonymousType method



JsonTextReader and JsonTextWriter

JsonConvert is useful but let's learn a little bit more about the control and performance that Json.NET provides to you via the JsonSerializer class. Let's start first with JsonReader and JsonWriter.


The JsonTextReader is for reading JSON. It's non-cache and forward only and it's used for large objects and files.

The JsonTextWriter which is used for creating JSON. It's also non-cached, forward only, and allows you to have a lot more control in improvements in performance.



Demo note

1. Using the JsonSerializer with the aid of a StreamWriter


serializer = new JsonSerializer();

serializer.NullValueHandling = NullValueHandling.Ignore; //setting to ignore null value when serializing

serialier.Formatting = Formatting.Indented;                   //indenting make file bigger

using (StreamWriter sw = new StreamWriter(@"..\..\jsonfilename.json"))

{

using (JsonWriter writer = new JsonTextWriter(sw))

{

serializer.Serialize(writer, author); //author is an instance to serialize

}

}


2. JsonConvert and ultimately the JsonSerializer class use reflection to convert from JsonText to .NET classes. Even though Json.NET is very fast by using reflection it makes it a little bit slower than it can actually be and thus we have the JsonTextReader, which does not use reflection and provides the fastest way of reading JSON text.


JsonTextReader jsonReader = new JsonTextReader(new StringReader(jsonText)); //jsonText is a string

while(jsonReader.Read()) //iterate strings in jsonText

{

if (jsonReader.Value != null) Console.WriteLine("Token: {0}, Value: {1}", jsonReader.TokenType, jsonReader.Value);

else ....skip...

}


TokenType : Start/EndObject, PropertyName, Date, Boolean, String, Integer, Start/EndArray... etc

The JsonTextWriter basically iterated over the entire jsonText, retrieving one token at a time. 


3. Write jsonText in a manual way for performance and control


StringWriter se = new StringWriter();

JsonTextWriter writer = new JsonTextWriter(sw);


writer.Formatting = Formatting.Indented; //at the beginning!


writer.WriterStartObject();

wirter.WritePropertyName("name");

writer.WriteValue("EMPister");

writer.WritePropertyName("courses");

writer.WriteStartArray();

writer.WriteValue("Json course");

writer.WriteEndArray();

writer.WritePropertyName("since");

writer.WriteValue(new DateTime(2018, 03, 29));

writer.WritePropertyName("happy");

writer.WriteValue(true);

writer.WritePropertyName("issues");

writer.WriteNull();

writer.WriterStartObject();

...

writer.WriteEndObject();

writer.WriteEndObject();

writer.Flush();


string jsonText = sw.GetStringBuilder().ToString();

//In .NET string are immutable so if you keep modifying a string without using a StringBuilder,

//you will be suffering from a deep performance hit



Dates in JSON

1. Without any setting, Json.NET's default is ISO 8601 (2009-07-11T23:00:00)


2. Use Microsoft date format with setting


JsonSerializerSetting settingsMicrosoftDate = new JsonSerializerSettings

{

DateFormatHandling = DateFormatHandling.MicrosoftDateFormat     // "\/Date(1247374800000-0600)\/"

};


3. Use custom date converter


... = JsonConvert.SerializeObject(author, Formatting.Indented, new IsoDateTimeConverter());


4. Use Custom format date


JsonSerializerSetting settingsCustomDate = new JsonSerializerSettings

{

DateFormatString = "dd/mm/yyyy"

};


5. Use JavaScript date


... = JsonConvert.SerializeObject(author, Formatting.Indented, new JavaScriptDateTimeConverter());  // "new Date(1247374800000)"



Error Handling


List<string> errors = new List<string>();

JsonSerializerSettings jSS = new JsonSerializerSettings

{

Error = delegate(object sender, ErrorEventArgs errorArgs)  //1 way

{

errors.Add(errorArgs.ErrorContext.Error.Message);

errorArgs.ErrorContext.Handled = true;

},

Error = HandleDeserializationError,                                //2 way

Converters = { new IsoDateTimeConverter() }

}


private static void HandleDeserializationError(object sender, ErrorEventArgs errorArgs)

{

var currentError = errorArgs.ErrorContext.Error.Message;

//Test if data in other format

errorArgs.ErrorContext.Handled = true;

}



or just throw error with try-catch block



출처

이 모든 내용은 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) Performance Tips  (0) 2018.03.30
(Json.NET) Custom Serialization  (0) 2018.03.30
(Json.NET) Settings & Attributes  (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