Skip to content

Commit

Permalink
feat: release version 2.2
Browse files Browse the repository at this point in the history
  • Loading branch information
adragonite committed Sep 28, 2017
1 parent 30637da commit aed6c4f
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 110 deletions.
129 changes: 69 additions & 60 deletions Assets/Plugins/UnityJSON/Deserializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,19 @@ public void DeserializeByParts (
_DeserializeByParts (obj, node, options, ignoredKeys);
}

/// <summary>
/// Deserializes the JSON string to a new object of the requested generic type. This
/// will first insantiate an object of the target type. The instantiation
/// will use the associated Instantiater for custom types. If an object can be
/// instantiated, #DeserializeOn method is used to feed the JSON into the object.
/// </summary>
/// <param name="jsonString">JSON string to deserialize.</param>
/// <param name="options">Deserialization options for the node (optional).</param>
public T Deserialize<T> (string jsonString, NodeOptions options = NodeOptions.Default)
{
return (T)Deserialize (jsonString, typeof(T), options);
}

/// <summary>
/// Deserializes the JSON string to a new object of the requested type. This
/// will first insantiate an object of the target type. The instantiation
Expand All @@ -202,11 +215,20 @@ public object Deserialize (
Type type,
NodeOptions options = NodeOptions.Default)
{
if (jsonString == null) {
throw new ArgumentNullException ("jsonString");
}
JSONNode node = SimpleJSON.JSON.Parse (jsonString);
return Deserialize (node, type, options);
return Deserialize (_ToJSONNode (jsonString), type, options);
}

/// <summary>
/// Deserializes the JSON node to a new object of the requested type. This
/// will first insantiate an object of the target type. The instantiation
/// will use the associated Instantiater for custom types. If an object can be
/// instantiated, #DeserializeOn method is used to feed the JSON into the object.
/// </summary>
/// <param name="node">JSON node to deserialize.</param>
/// <param name="options">Deserialization options for the node (optional).</param>
public T Deserialize<T> (JSONNode node, NodeOptions options = NodeOptions.Default)
{
return (T)Deserialize (node, typeof(T), options);
}

/// <summary>
Expand Down Expand Up @@ -246,11 +268,11 @@ public object DeserializeToObject (
Type[] customTypes = null,
NodeOptions options = NodeOptions.Default)
{
if (jsonString == null) {
throw new ArgumentNullException ("jsonString");
}
JSONNode node = SimpleJSON.JSON.Parse (jsonString);
return DeserializeToObject (jsonString, restrictedTypes, customTypes, options);
return DeserializeToObject (
_ToJSONNode (jsonString),
restrictedTypes,
customTypes,
options);
}

/// <summary>
Expand Down Expand Up @@ -293,11 +315,7 @@ public Nullable<T> DeserializeToNullable<T> (
string jsonString,
NodeOptions options = NodeOptions.Default) where T : struct
{
if (jsonString == null) {
throw new ArgumentNullException ("jsonString");
}
JSONNode node = SimpleJSON.JSON.Parse (jsonString);
return DeserializeToNullable<T> (node, options);
return DeserializeToNullable<T> (_ToJSONNode (jsonString), options);
}

/// <summary>
Expand Down Expand Up @@ -325,11 +343,7 @@ public int DeserializeToInt (
string jsonString,
NodeOptions options = NodeOptions.Default)
{
if (jsonString == null) {
throw new ArgumentNullException ("jsonString");
}
JSONNode node = SimpleJSON.JSON.Parse (jsonString);
return DeserializeToInt (node, options);
return DeserializeToInt (_ToJSONNode (jsonString), options);
}

/// <summary>
Expand All @@ -356,11 +370,7 @@ public uint DeserializeToUInt (
string jsonString,
NodeOptions options = NodeOptions.Default)
{
if (jsonString == null) {
throw new ArgumentNullException ("jsonString");
}
JSONNode node = SimpleJSON.JSON.Parse (jsonString);
return DeserializeToUInt (node, options);
return DeserializeToUInt (_ToJSONNode (jsonString), options);
}

/// <summary>
Expand All @@ -387,11 +397,7 @@ public byte DeserializeToByte (
string jsonString,
NodeOptions options = NodeOptions.Default)
{
if (jsonString == null) {
throw new ArgumentNullException ("jsonString");
}
JSONNode node = SimpleJSON.JSON.Parse (jsonString);
return DeserializeToByte (node, options);
return DeserializeToByte (_ToJSONNode (jsonString), options);
}

/// <summary>
Expand All @@ -418,11 +424,7 @@ public bool DeserializeToBool (
string jsonString,
NodeOptions options = NodeOptions.Default)
{
if (jsonString == null) {
throw new ArgumentNullException ("jsonString");
}
JSONNode node = SimpleJSON.JSON.Parse (jsonString);
return DeserializeToBool (node, options);
return DeserializeToBool (_ToJSONNode (jsonString), options);
}

/// <summary>
Expand All @@ -449,11 +451,7 @@ public float DeserializeToFloat (
string jsonString,
NodeOptions options = NodeOptions.Default)
{
if (jsonString == null) {
throw new ArgumentNullException ("jsonString");
}
JSONNode node = SimpleJSON.JSON.Parse (jsonString);
return DeserializeToFloat (node, options);
return DeserializeToFloat (_ToJSONNode (jsonString), options);
}

/// <summary>
Expand All @@ -480,11 +478,7 @@ public double DeserializeToDouble (
string jsonString,
NodeOptions options = NodeOptions.Default)
{
if (jsonString == null) {
throw new ArgumentNullException ("jsonString");
}
JSONNode node = SimpleJSON.JSON.Parse (jsonString);
return DeserializeToDouble (node, options);
return DeserializeToDouble (_ToJSONNode (jsonString), options);
}

/// <summary>
Expand All @@ -511,11 +505,7 @@ public long DeserializeToLong (
string jsonString,
NodeOptions options = NodeOptions.Default)
{
if (jsonString == null) {
throw new ArgumentNullException ("jsonString");
}
JSONNode node = SimpleJSON.JSON.Parse (jsonString);
return DeserializeToLong (node, options);
return DeserializeToLong (_ToJSONNode (jsonString), options);
}

/// <summary>
Expand All @@ -541,11 +531,7 @@ public string DeserializeToString (
string jsonString,
NodeOptions options = NodeOptions.Default)
{
if (jsonString == null) {
throw new ArgumentNullException ("jsonString");
}
JSONNode node = SimpleJSON.JSON.Parse (jsonString);
return DeserializeToString (node, options);
return DeserializeToString (_ToJSONNode (jsonString), options);
}

/// <summary>
Expand Down Expand Up @@ -573,11 +559,7 @@ public T DeserializeToEnum<T> (
string jsonString,
NodeOptions options = NodeOptions.Default)
{
if (jsonString == null) {
throw new ArgumentNullException ("jsonString");
}
JSONNode node = SimpleJSON.JSON.Parse (jsonString);
return DeserializeToEnum<T> (node, options);
return DeserializeToEnum<T> (_ToJSONNode (jsonString), options);
}

/// <summary>
Expand Down Expand Up @@ -812,6 +794,33 @@ internal object Deserialize (
return _DeserializeToCustom (node, targetType, options);
}

private JSONNode _ToJSONNode (string jsonString)
{
if (jsonString == null) {
throw new ArgumentNullException ("jsonString");
}

JSONNode node = SimpleJSON.JSON.Parse (jsonString);
if (node != null) {
return node;
} else {
double value;
if (jsonString == "true") {
return new JSONBool (true);
} else if (jsonString == "false") {
return new JSONBool (false);
} else if (jsonString == "null" || jsonString == "undefined") {
return new JSONNull ();
} else if (double.TryParse (jsonString, out value)) {
return new JSONNumber (value);
} else if (jsonString.StartsWith ("\"") && jsonString.EndsWith ("\"")) {
return new JSONString (jsonString.Substring (1, jsonString.Length - 2));
} else {
return new JSONString (jsonString);
}
}
}

private object _Deserialize (
JSONNode node,
Type type,
Expand Down
46 changes: 4 additions & 42 deletions Assets/Plugins/UnityJSON/JSON.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,18 +93,10 @@ public static T Deserialize<T> (
NodeOptions options = NodeOptions.Default,
Deserializer deserializer = null)
{
if (jsonString == null) {
throw new ArgumentNullException ("jsonString");
}
if (deserializer == null) {
deserializer = Deserializer.Default;
}

SimpleJSON.JSONNode node = SimpleJSON.JSON.Parse (jsonString);
if (node == null) {
throw new ArgumentException ("Argument is not a valid JSON string: " + jsonString);
}
return (T)deserializer.Deserialize (node, typeof(T), options);
return deserializer.Deserialize<T> (jsonString, options);
}

/// <summary>
Expand All @@ -120,18 +112,10 @@ public static T Deserialize<T> (
string jsonString,
Deserializer deserializer)
{
if (jsonString == null) {
throw new ArgumentNullException ("jsonString");
}
if (deserializer == null) {
throw new ArgumentNullException ("deserializer");
}

SimpleJSON.JSONNode node = SimpleJSON.JSON.Parse (jsonString);
if (node == null) {
throw new ArgumentException ("Argument is not a valid JSON string: " + jsonString);
}
return (T)deserializer.Deserialize (node, typeof(T));
return deserializer.Deserialize<T> (jsonString);
}

/// <summary>
Expand All @@ -149,21 +133,10 @@ public static void DeserializeOn (
NodeOptions options = NodeOptions.Default,
Deserializer deserializer = null)
{
if (obj == null) {
throw new ArgumentNullException ("obj");
}
if (jsonString == null) {
throw new ArgumentNullException ("jsonString");
}
if (deserializer == null) {
deserializer = Deserializer.Default;
}

SimpleJSON.JSONNode node = SimpleJSON.JSON.Parse (jsonString);
if (node == null) {
throw new ArgumentException ("Argument is not a valid JSON string: " + jsonString);
}
deserializer.DeserializeOn (obj, node, options);
deserializer.DeserializeOn (obj, jsonString, options);
}

/// <summary>
Expand All @@ -179,21 +152,10 @@ public static void DeserializeOn (
string jsonString,
Deserializer deserializer)
{
if (obj == null) {
throw new ArgumentNullException ("obj");
}
if (jsonString == null) {
throw new ArgumentNullException ("jsonString");
}
if (deserializer == null) {
throw new ArgumentNullException ("deserializer");
}

SimpleJSON.JSONNode node = SimpleJSON.JSON.Parse (jsonString);
if (node == null) {
throw new ArgumentException ("Argument is not a valid JSON string: " + jsonString);
}
deserializer.DeserializeOn (obj, node);
deserializer.DeserializeOn (obj, jsonString);
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion Assets/Plugins/UnityJSON/Serializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public static Serializer Default {
/// <summary>
/// The initial serializer that is provided by the framework.
/// </summary>
private static readonly Serializer Simple = new Serializer ();
public static readonly Serializer Simple = new Serializer ();

/// <summary>
/// When set to true, the keyword undefined is used instead of null
Expand Down
17 changes: 10 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
- [Enums](#enums)
- [Tuples](#tuples)
* [Changelog](#changelog)
- [v2.0](#v20)
- [v1.1](#v11)

## Features

Expand Down Expand Up @@ -691,19 +689,24 @@ UnityJSON does not use C# tuples because Unity3D does not have support for them

## Changelog

### v2.2

- Bug fixes
- Adds generic deserialize methods to `Deserializer`

### v2.1

- Provides Tuple support

### v2.0

- Bug fixes
- Adds Serializer.SerializeByParts
- Adds Deserializer.DeserializeByParts and deserializer methods taking JSON
- Adds `Serializer.SerializeByParts`
- Adds `Deserializer.DeserializeByParts` and deserializer methods taking JSON
string arguments
- Creates the class Instantiater
- Allows use of RestrictTypeAttribute with constructor arguments
- Introduces InstantiationData to work around ignored keys
- Creates the class `Instantiater`
- Allows use of `RestrictTypeAttribute` with constructor arguments
- Introduces `InstantiationData` to work around ignored keys

### v1.1

Expand Down

0 comments on commit aed6c4f

Please sign in to comment.