Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Join us on March 30, 2023, between 5 am & 1 pm EST, in the Performance Profiling Dev Blitz Day 2023 - Q&A forum and Discord where you can connect with our teams behind the Memory and CPU Profilers.
    Dismiss Notice

JsonFormatter - Complete Json Serialization Library for Unity

Discussion in 'Assets and Asset Store' started by hasanbayat, Sep 7, 2017.

  1. hasanbayat

    hasanbayat

    Joined:
    Oct 18, 2016
    Posts:
    626
    Hi.

    JsonFormatter is a Fast, Lightweight JSON serialization/deserialization library for Unity projects.

    Download
    Features
    • Serializing Collections: Lists, Dictionaries, IEnumerable
    • Serializing KeyValuePair
    • Serializing ISerializable
    • Surrogate Serialization
    • Serializing Almost anything (Automatically serializes public fields and properties)
    • Deserializing IDeserializationCallback
    • Fast and Helpful Customer Support
    • Free & Open Source
    • Easy to Use
    • Cross Platform (Let us know if you have any problem with any platform)

    Getting Started
    Just add

    Code (CSharp):
    1. using BayatGames.Serialization.Formatters.Json;
    then you are ready to go.

    JsonFormatter provides some static methods for fast serialization of objects to json string:

    Code (CSharp):
    1. using BayatGames.Serialization.Formatters.Json;
    2. ...
    3. string json = JsonFormatter.SerializeObject ("Hello World");
    Resources
    License

    MIT @ Bayat Games
     
    Last edited: Nov 28, 2017
  2. alandean

    alandean

    Joined:
    Mar 16, 2015
    Posts:
    4
    I have information pulled from a restAPI, how would I use this to deserialise that information?
     
  3. hasanbayat

    hasanbayat

    Joined:
    Oct 18, 2016
    Posts:
    626
    Could you post an example of response?

    You can use JsonFormatter.DeserializeObject(string json, typeof(Dictionary<string, string>)).

    Also you can make a storage object to store the response in a pre-defined object and use it for deserialization.
     
  4. antsonthetree

    antsonthetree

    Joined:
    May 15, 2015
    Posts:
    101
    Hello - I am trying to use the JsonFormatter with a simple Vector3 surrogate, but it does not seem to serialize the Vec3. I am using the following to setup and test the surrogate but not having much success

    Code (CSharp):
    1.  public void Save()
    2.     {
    3.         JsonFormatter formatter = new JsonFormatter();
    4.         SurrogateSelector ss = new SurrogateSelector();
    5.         Vector3Surrogate Vector3_SS = new Vector3Surrogate();
    6.         ss.AddSurrogate(typeof(Vector3), new StreamingContext(StreamingContextStates.All), Vector3_SS);
    7.         formatter.surrogateSelector = ss;
    8.         var saveData = new MyClass();
    9.  
    10.         string json = formatter.Serialize(saveData);
    11.         Debug.Log(json);
    12.     }
    13.  
    14.     public class MyClass
    15.     {
    16.         public string Name = "test";
    17.         public Vector3 Vector;
    18.  
    19.         public MyClass()
    20.         {
    21.             Vector = new Vector3(1f, 2f, 3f);
    22.         }
    23.     }
     
  5. hasanbayat

    hasanbayat

    Joined:
    Oct 18, 2016
    Posts:
    626
    Hi.
    Are you getting any errors?

    Thanks.
     
  6. antsonthetree

    antsonthetree

    Joined:
    May 15, 2015
    Posts:
    101
    I am not getting an error. I just get an empty json string as output from the serialize call.
    Also for reference here is my surrogate class:

    Code (CSharp):
    1.     class Vector3Surrogate : ISerializationSurrogate
    2.     {
    3.         // Method called to serialize a Vector3 object
    4.         public void GetObjectData(System.Object obj, SerializationInfo info, StreamingContext context)
    5.         {
    6.             Vector3 vector = (Vector3) obj;
    7.             info.AddValue("x", vector.x);
    8.             info.AddValue("y", vector.y);
    9.             info.AddValue("z", vector.z);
    10.         }
    11.  
    12.         // Method called to deserialize a Vector3 object
    13.         public System.Object SetObjectData(System.Object obj, SerializationInfo info, StreamingContext context, ISurrogateSelector selector)
    14.         {
    15.             return new UnityEngine.Vector3(info.GetSingle("x"), info.GetSingle("y"), info.GetSingle("z"));
    16.         }
    17.     }
     
  7. Mobi2FunUnity

    Mobi2FunUnity

    Joined:
    Nov 21, 2016
    Posts:
    1
    Getting FormatException: error while deserializing can any one plz help.

    Error description :
    FormatException: Input string was not in a correct format.
    System.Number.StringToNumber (System.String str, System.Globalization.NumberStyles options, System.Number+NumberBuffer& number, System.Globalization.NumberFormatInfo info, System.Boolean parseDecimal) (at <ac823e2bb42b41bda67924a45a0173c3>:0)
    System.Number.ParseInt32 (System.String s, System.Globalization.NumberStyles style, System.Globalization.NumberFormatInfo info) (at <ac823e2bb42b41bda67924a45a0173c3>:0)
    System.Int32.Parse (System.String s, System.Globalization.NumberStyles style, System.IFormatProvider provider) (at <ac823e2bb42b41bda67924a45a0173c3>:0)
    System.Convert.ToInt32 (System.String value, System.IFormatProvider provider) (at <ac823e2bb42b41bda67924a45a0173c3>:0)
    System.String.System.IConvertible.ToInt32 (System.IFormatProvider provider) (at <ac823e2bb42b41bda67924a45a0173c3>:0)
    System.Convert.ChangeType (System.Object value, System.Type conversionType, System.IFormatProvider provider) (at <ac823e2bb42b41bda67924a45a0173c3>:0)
    System.Convert.ChangeType (System.Object value, System.Type conversionType) (at <ac823e2bb42b41bda67924a45a0173c3>:0)
    BayatGames.Serialization.Formatters.Json.JsonTextReader.Read (System.Type type, System.String json) (at Assets/BayatGames/JsonFormatter/Scripts/JsonTextReader.cs:110)
    BayatGames.Serialization.Formatters.Json.JsonTextReader.Read (System.Type type, System.String json) (at Assets/BayatGames/JsonFormatter/Scripts/JsonTextReader.cs:119)
    BayatGames.Serialization.Formatters.Json.JsonTextReader.ReadObject (System.Type type, System.String json) (at Assets/BayatGames/JsonFormatter/Scripts/JsonTextReader.cs:238)
    BayatGames.Serialization.Formatters.Json.JsonTextReader.Read (System.Type type, System.String json) (at Assets/BayatGames/JsonFormatter/Scripts/JsonTextReader.cs:185)
    BayatGames.Serialization.Formatters.Json.JsonTextReader.Read (System.Type type, System.String json) (at Assets/BayatGames/JsonFormatter/Scripts/JsonTextReader.cs:119)
    BayatGames.Serialization.Formatters.Json.JsonTextReader.ReadObject (System.Type type, System.String json) (at Assets/BayatGames/JsonFormatter/Scripts/JsonTextReader.cs:238)
    BayatGames.Serialization.Formatters.Json.JsonTextReader.Read (System.Type type, System.String json) (at Assets/BayatGames/JsonFormatter/Scripts/JsonTextReader.cs:185)
    BayatGames.Serialization.Formatters.Json.JsonTextReader.Read (System.Type type, System.String json) (at Assets/BayatGames/JsonFormatter/Scripts/JsonTextReader.cs:119)
    BayatGames.Serialization.Formatters.Json.JsonTextReader.ReadObject (System.Type type, System.String json) (at Assets/BayatGames/JsonFormatter/Scripts/JsonTextReader.cs:238)
    BayatGames.Serialization.Formatters.Json.JsonTextReader.Read (System.Type type, System.String json) (at Assets/BayatGames/JsonFormatter/Scripts/JsonTextReader.cs:185)
    BayatGames.Serialization.Formatters.Json.JsonTextReader.ReadObject (System.Type type, System.String json) (at Assets/BayatGames/JsonFormatter/Scripts/JsonTextReader.cs:238)
    BayatGames.Serialization.Formatters.Json.JsonTextReader.Read (System.Type type, System.String json) (at Assets/BayatGames/JsonFormatter/Scripts/JsonTextReader.cs:185)
    BayatGames.Serialization.Formatters.Json.JsonTextReader.Read (System.Type type) (at Assets/BayatGames/JsonFormatter/Scripts/JsonTextReader.cs:74)
    BayatGames.Serialization.Formatters.Json.JsonFormatter.Deserialize (System.IO.TextReader input, System.Type type) (at Assets/BayatGames/JsonFormatter/Scripts/JsonFormatter.cs:184)
    BayatGames.Serialization.Formatters.Json.JsonFormatter.Deserialize (System.String input, System.Type type) (at Assets/BayatGames/JsonFormatter/Scripts/JsonFormatter.cs:162)
    BayatGames.Serialization.Formatters.Json.JsonFormatter.DeserializeObject (System.String json, System.Type type) (at Assets/BayatGames/JsonFormatter/Scripts/JsonFormatter.cs:208)
    SnakeColor.NewScripts.DataController.LoadLevleData () (at Assets/SnakeColor/New Scripts/DataController.cs:35)
    SnakeColor.NewScripts.DataController.Start () (at Assets/SnakeColor/New Scripts/DataController.cs:21)


    Code (CSharp):
    1.  
    2.  
    3.  
    4. public class GameLevelData
    5.     {
    6.         public LevelData levelData;
    7.     }
    8.  
    9.     public class DataController : MonoBehaviour
    10.     {
    11.         public LevelData levelData;
    12.  
    13.         private string _levelDataFileName = "Level_01.json";
    14.  
    15.         private void Start()
    16.         {
    17.             LoadLevleData();
    18.  
    19.         }
    20.  
    21.         private void LoadLevleData()
    22.         {
    23.             var filePath = Path.Combine(Application.streamingAssetsPath, _levelDataFileName);
    24.  
    25.             if (File.Exists(filePath))
    26.             {
    27.                 var dataAsJson = File.ReadAllText(filePath);
    28.  
    29.                 Debug.Log("...................      " + dataAsJson.ToLower());
    30.        
    31.               var loadedData = (GameLevelData)JsonFormatter.DeserializeObject(dataAsJson, typeof(GameLevelData));
    32.  
    33.                 Debug.Log("...........        ........      " + loadedData);
    34.                 levelData = loadedData.levelData;
    35.              
    36.             }
    37.             else
    38.             {
    39.                 Debug.LogError("Cannot load game data");
    40.             }
    41.         }
    42.     }
    43. }