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
using BayatGames.Serialization.Formatters.Json;
then you are ready to go.
JsonFormatter provides some static methods for fast serialization of objects to json string:
using BayatGames.Serialization.Formatters.Json;
...
string json = JsonFormatter.SerializeObject ("Hello World");
Resources
License
MIT @ Bayat Games
I have information pulled from a restAPI, how would I use this to deserialise that information?
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.
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
public void Save()
{
JsonFormatter formatter = new JsonFormatter();
SurrogateSelector ss = new SurrogateSelector();
Vector3Surrogate Vector3_SS = new Vector3Surrogate();
ss.AddSurrogate(typeof(Vector3), new StreamingContext(StreamingContextStates.All), Vector3_SS);
formatter.surrogateSelector = ss;
var saveData = new MyClass();
string json = formatter.Serialize(saveData);
Debug.Log(json);
}
public class MyClass
{
public string Name = "test";
public Vector3 Vector;
public MyClass()
{
Vector = new Vector3(1f, 2f, 3f);
}
}
antsonthetree:
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
public void Save()
{
JsonFormatter formatter = new JsonFormatter();
SurrogateSelector ss = new SurrogateSelector();
Vector3Surrogate Vector3_SS = new Vector3Surrogate();
ss.AddSurrogate(typeof(Vector3), new StreamingContext(StreamingContextStates.All), Vector3_SS);
formatter.surrogateSelector = ss;
var saveData = new MyClass();
string json = formatter.Serialize(saveData);
Debug.Log(json);
}
public class MyClass
{
public string Name = "test";
public Vector3 Vector;
public MyClass()
{
Vector = new Vector3(1f, 2f, 3f);
}
}
Hi.
Are you getting any errors?
Thanks.
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:
class Vector3Surrogate : ISerializationSurrogate
{
// Method called to serialize a Vector3 object
public void GetObjectData(System.Object obj, SerializationInfo info, StreamingContext context)
{
Vector3 vector = (Vector3) obj;
info.AddValue("x", vector.x);
info.AddValue("y", vector.y);
info.AddValue("z", vector.z);
}
// Method called to deserialize a Vector3 object
public System.Object SetObjectData(System.Object obj, SerializationInfo info, StreamingContext context, ISurrogateSelector selector)
{
return new UnityEngine.Vector3(info.GetSingle("x"), info.GetSingle("y"), info.GetSingle("z"));
}
}
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 :0)
System.Number.ParseInt32 (System.String s, System.Globalization.NumberStyles style, System.Globalization.NumberFormatInfo info) (at :0)
System.Int32.Parse (System.String s, System.Globalization.NumberStyles style, System.IFormatProvider provider) (at :0)
System.Convert.ToInt32 (System.String value, System.IFormatProvider provider) (at :0)
System.String.System.IConvertible.ToInt32 (System.IFormatProvider provider) (at :0)
System.Convert.ChangeType (System.Object value, System.Type conversionType, System.IFormatProvider provider) (at :0)
System.Convert.ChangeType (System.Object value, System.Type conversionType) (at :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)
public class GameLevelData
{
public LevelData levelData;
}
public class DataController : MonoBehaviour
{
public LevelData levelData;
private string _levelDataFileName = "Level_01.json";
private void Start()
{
LoadLevleData();
}
private void LoadLevleData()
{
var filePath = Path.Combine(Application.streamingAssetsPath, _levelDataFileName);
if (File.Exists(filePath))
{
var dataAsJson = File.ReadAllText(filePath);
Debug.Log("................... " + dataAsJson.ToLower());
var loadedData = (GameLevelData)JsonFormatter.DeserializeObject(dataAsJson, typeof(GameLevelData));
Debug.Log("........... ........ " + loadedData);
levelData = loadedData.levelData;
}
else
{
Debug.LogError("Cannot load game data");
}
}
}
}