Skip to content

Instantly share code, notes, and snippets.

@whoo24
Last active April 25, 2018 10:18
Show Gist options
  • Save whoo24/5506804 to your computer and use it in GitHub Desktop.
Save whoo24/5506804 to your computer and use it in GitHub Desktop.
LitJson simple samples
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"]);
...
JsonMapper.RegisterImporter<Int32, DateTime>(int32toMillsecond);
...
public static DateTime int32toMillsecond(Int32 value)
{
return DateTime.Today.AddHours(value);
}
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()
}
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);
}
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());
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);
}
customReader = new JsonReader(json_array);
customReader.AllowComments = false; // Json text 내에서 주석의 허용 여부
customReader.AllowSingleQuotedStrings = false; // 인용부호(')의 허용 여부
customReader.SkipNonMembers = false; // 오브젝트 맴버와 Json Text가 1:1 매치안되는 상황에서의 예외 발생여부
JsonMapper.ToObject(customReader);
JsonWriter writer = new JsonWriter(json);
writer.PrettyPrint = true; // 한줄로 JsonText를 생성하지않고 사람이 읽기 쉽게 출력
writer.IndentValue = 2; // 들여쓰기
JsonMapper.ToJson(obj, writer);
@bluemonkmn
Copy link

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.

ThirdParty.Json.LitJson.JsonMapper.RegisterExporter<DateTime>((d, w) => w.Write(d.Ticks.ToString()));
ThirdParty.Json.LitJson.JsonMapper.RegisterImporter<long, DateTime>((j) => new DateTime(j));
ThirdParty.Json.LitJson.JsonMapper.RegisterImporter<string, DateTime>((j) => new DateTime(long.Parse(j)));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment