Skip to content

Repository files navigation

Tiny-JSON /L

C# JSON serializer, that works fine with Unity3D.

Fork by Lisias.

In a Hurry

Description

Tiny-JSON is a pretty small and practical JSON (de)serializer for C#, working fine under C# 3.5 and Unity Engine.

This fork aims to customize and/or tailor it to fit KSPe needs.

encoding

// Encode a Dictionaryvardict=newDictionary<string,int>{{"three",3},{"five",5},{"ten",10}};stringjson=Json.Encode(dict);// Encode a Unity3D type (or any type)varv2=newVector2(3,5);stringjson=v2.Encode();

decoding

// Decoding a Dictionary from a json stringvardict=Json.Decode<Dictionary<string,int>>(json);// Decoding a Vector2varvector=Json.Decode<Vector2>("{\"x\":4, \"y\":2}");// Decoding a list of Vector3varvectors=Json.Decode<IList<Vector3>>("[{\"x\":4, \"y\":3, \"z\":-1}, {\"x\":1, \"y\":1, \"z\":1}, {}]");

And support for custom fields, very handy if you connect to an external service and don't want to match your naming style:

[Serializable]publicclassSession{[JsonProperty("token_type")]publicstringtokenType;[JsonProperty("access_token")]publicstringaccessToken;[JsonProperty("refresh_token")]publicstringrefreshToken;[JsonProperty("expires_in")]publiclongaccessTokenExpire;}// ... decode with custom attributed fieldsbyte[]result=request.downloadHandler.data;stringjson=System.Text.Encoding.Default.GetString(result);returnjson.Decode<Session>();

Or even simpler, automatically match snake case to camel case:

[MatchSnakeCase]publicclassSession{publicstringtokenType;publicstringaccessToken;publicstringrefreshToken;[JsonProperty("expires_in")]publiclongaccessTokenExpire;}// ... decode with custom attributed fieldsbyte[]result=request.downloadHandler.data;stringjson=System.Text.Encoding.Default.GetString(result);returnjson.Decode<Session>();

License:

Licensed under the MIT (Expat).

UPSTREAM

About

C# JSON serializer, that works fine with Unity3D

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages