diff --git a/Assets/Plugins/UnityJSON/Deserializer.cs b/Assets/Plugins/UnityJSON/Deserializer.cs
index 142107b..b122a1a 100644
--- a/Assets/Plugins/UnityJSON/Deserializer.cs
+++ b/Assets/Plugins/UnityJSON/Deserializer.cs
@@ -188,6 +188,19 @@ public void DeserializeByParts (
_DeserializeByParts (obj, node, options, ignoredKeys);
}
+ ///
+ /// 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.
+ ///
+ /// JSON string to deserialize.
+ /// Deserialization options for the node (optional).
+ public T Deserialize (string jsonString, NodeOptions options = NodeOptions.Default)
+ {
+ return (T)Deserialize (jsonString, typeof(T), options);
+ }
+
///
/// Deserializes the JSON string to a new object of the requested type. This
/// will first insantiate an object of the target type. The instantiation
@@ -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);
+ }
+
+ ///
+ /// 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.
+ ///
+ /// JSON node to deserialize.
+ /// Deserialization options for the node (optional).
+ public T Deserialize (JSONNode node, NodeOptions options = NodeOptions.Default)
+ {
+ return (T)Deserialize (node, typeof(T), options);
}
///
@@ -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);
}
///
@@ -293,11 +315,7 @@ public Nullable DeserializeToNullable (
string jsonString,
NodeOptions options = NodeOptions.Default) where T : struct
{
- if (jsonString == null) {
- throw new ArgumentNullException ("jsonString");
- }
- JSONNode node = SimpleJSON.JSON.Parse (jsonString);
- return DeserializeToNullable (node, options);
+ return DeserializeToNullable (_ToJSONNode (jsonString), options);
}
///
@@ -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);
}
///
@@ -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);
}
///
@@ -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);
}
///
@@ -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);
}
///
@@ -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);
}
///
@@ -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);
}
///
@@ -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);
}
///
@@ -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);
}
///
@@ -573,11 +559,7 @@ public T DeserializeToEnum (
string jsonString,
NodeOptions options = NodeOptions.Default)
{
- if (jsonString == null) {
- throw new ArgumentNullException ("jsonString");
- }
- JSONNode node = SimpleJSON.JSON.Parse (jsonString);
- return DeserializeToEnum (node, options);
+ return DeserializeToEnum (_ToJSONNode (jsonString), options);
}
///
@@ -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,
diff --git a/Assets/Plugins/UnityJSON/JSON.cs b/Assets/Plugins/UnityJSON/JSON.cs
index 44c1fd8..d8106f1 100644
--- a/Assets/Plugins/UnityJSON/JSON.cs
+++ b/Assets/Plugins/UnityJSON/JSON.cs
@@ -93,18 +93,10 @@ public static T Deserialize (
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 (jsonString, options);
}
///
@@ -120,18 +112,10 @@ public static T Deserialize (
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 (jsonString);
}
///
@@ -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);
}
///
@@ -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);
}
///
diff --git a/Assets/Plugins/UnityJSON/Serializer.cs b/Assets/Plugins/UnityJSON/Serializer.cs
index 8966a84..8eefb22 100644
--- a/Assets/Plugins/UnityJSON/Serializer.cs
+++ b/Assets/Plugins/UnityJSON/Serializer.cs
@@ -38,7 +38,7 @@ public static Serializer Default {
///
/// The initial serializer that is provided by the framework.
///
- private static readonly Serializer Simple = new Serializer ();
+ public static readonly Serializer Simple = new Serializer ();
///
/// When set to true, the keyword undefined is used instead of null
diff --git a/README.md b/README.md
index d7be595..a2ab1dc 100644
--- a/README.md
+++ b/README.md
@@ -23,8 +23,6 @@
- [Enums](#enums)
- [Tuples](#tuples)
* [Changelog](#changelog)
- - [v2.0](#v20)
- - [v1.1](#v11)
## Features
@@ -691,6 +689,11 @@ 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
@@ -698,12 +701,12 @@ UnityJSON does not use C# tuples because Unity3D does not have support for them
### 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