C# JSON serializer, that works fine with Unity3D
// 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 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>();