Wanzyee Studio

Integrate custom Newtonsoft.Json.JsonConverter and minor tweaks to use Json.NET in Unity. More...

Static Public Attributes

static readonly IList< JsonConverter > converters = CreateConverters()
 The default custom converters. More...
 
static readonly JsonSerializerSettings defaultSettings
 The default Newtonsoft.Json.JsonSerializerSettings. More...
 

Detailed Description

Integrate custom Newtonsoft.Json.JsonConverter and minor tweaks to use Json.NET in Unity.

To use Json.NET, please set Unity "PlayerSettings/Api Compatibility Lavel" to .NET 2.0. Then download from its website and import the .NET 2.0 dll. The defaultSettings is assigned to JsonConvert.DefaultSettings when initializing.

The defaultSettings with the tweaks below:

  1. Include custom converters to support serializing some Unity types, e.g., UnityEngine.Vector3.
  2. Use DataContractResolver to make sure serializing custom class as expection.
  3. Use ObjectCreationHandling.Replace to make sure deserialized data identical with the text.

Now we can use Json.NET just like before:

Debug.Log(JsonConvert.SerializeObject(Vector3.up));
var vec = JsonConvert.DeserializeObject<Vector2>("{'x':1.0,'y':0.0}");

User can directly modify defaultSettings for customization, and override it:

JsonConvert.DefaultSettings = () => new JsonSerializerSettings(){
Converters = JsonNetUtility.converters,
ContractResolver = DataContractResolver.instance
};

Member Data Documentation

readonly IList<JsonConverter> converters = CreateConverters()
static

The default custom converters.

Includes below:

  1. Any custom Newtonsoft.Json.JsonConverter has constructor without parameters.
  2. Any Newtonsoft.Json.JsonConverter from WanzyeeStudio.Json.
  3. Newtonsoft.Json.Converters.StringEnumConverter.
  4. Newtonsoft.Json.Converters.VersionConverter.
readonly JsonSerializerSettings defaultSettings
static

The default Newtonsoft.Json.JsonSerializerSettings.