Last active
April 25, 2018 10:18
-
-
Save whoo24/5506804 to your computer and use it in GitHub Desktop.
LitJson simple samples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
string json_text = @" | |
{ | |
""album"" : { | |
""name"" : ""The Dark Side of the Moon"", | |
""artist"" : ""Pink Floyd"", | |
""year"" : 1973, | |
""tracks"" : [ | |
""Speak To Me"", | |
""Breathe"", | |
""On The Run"" | |
] | |
} | |
} | |
"; | |
JsonData data = JsonMapper.ToObject(json_text); | |
Console.WriteLine("Album's name: {0}", data["album"]["name"]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
JsonMapper.RegisterImporter<Int32, DateTime>(int32toMillsecond); | |
... | |
public static DateTime int32toMillsecond(Int32 value) | |
{ | |
return DateTime.Today.AddHours(value); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
string json = @"{ | |
""name"" : ""Bill"", | |
""age"" : 32, | |
""awake"" : true, | |
""n"" : 1994.0226, | |
""note"" : [ ""life"", ""is"", ""but"", ""a"", ""dream"" ] | |
}" | |
JsonReader reader = new JsonReader(json); | |
while (reader.Read()) { | |
// reader.Token, reader.Value, reader.Value.GetType() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using LitJson; | |
public class Person | |
{ | |
public string Name { get; set; } | |
public int Age { get; set; } | |
public DateTime Birthday { get; set; } | |
} | |
public static void JsonToPerson() | |
{ | |
string json = @"{ | |
""Name"" : ""Thomas More"", | |
""Age"" : 57, | |
""Birthday"" : ""02/07/1478 00:00:00"" | |
}"; | |
Person thomas = JsonMapper.ToObject(json); | |
Console.WriteLine("Thomas' age: {0}", thomas.Age); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
StringBuilder sb = new StringBuilder(); | |
JsonWriter writer = new JsonWriter(sb); | |
writer.WriteArrayStart(); | |
writer.Write(1); | |
writer.Write(2); | |
writer.Write(3); | |
writer.WriteObjectStart(); | |
writer.WritePropertyName("color"); | |
writer.Write("blue"); | |
writer.WriteObjectEnd(); | |
writer.WriteArrayEnd(); | |
Console.WriteLine(sb.ToString()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using LitJson; | |
public class Person | |
{ | |
public string Name { get; set; } | |
public int Age { get; set; } | |
public DateTime Birthday { get; set; } | |
} | |
public static void PersonToJson() | |
{ | |
Person bill = new Person(); | |
bill.Name = "William Shakespeare"; | |
bill.Age = 51; | |
bill.Birthday = new DateTime(1564, 4, 26); | |
string json_bill = JsonMapper.ToJson(bill); | |
Console.WriteLine(json_bill); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
customReader = new JsonReader(json_array); | |
customReader.AllowComments = false; // Json text 내에서 주석의 허용 여부 | |
customReader.AllowSingleQuotedStrings = false; // 인용부호(')의 허용 여부 | |
customReader.SkipNonMembers = false; // 오브젝트 맴버와 Json Text가 1:1 매치안되는 상황에서의 예외 발생여부 | |
JsonMapper.ToObject(customReader); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
JsonWriter writer = new JsonWriter(json); | |
writer.PrettyPrint = true; // 한줄로 JsonText를 생성하지않고 사람이 읽기 쉽게 출력 | |
writer.IndentValue = 2; // 들여쓰기 | |
JsonMapper.ToJson(obj, writer); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I found odd behavior with long integers like the size you get with a typical .NET DateTime Ticks value. I had to RegisterImporter for strings because I guess the number was too long for the parser to treat as a number, even though it fits in a Int64/long.