Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/mono/Newtonsoft.Json.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJamesNK <james@newtonking.com>2010-02-07 01:55:36 +0300
committerJamesNK <james@newtonking.com>2010-02-07 01:55:36 +0300
commitc7facae78977adc3ff57aff214d2beed9039fbcf (patch)
tree8062b4e6482ca4f627dc86fc4c50cfa493f7d1ca /Src/Newtonsoft.Json
parent992985bd7b981bef99a6d979446128a9d40a6607 (diff)
-Added GetSchema, CanRead, CanWrite to JsonConverter
-Fix CustomCreationConverter erroring when writing JSON
Diffstat (limited to 'Src/Newtonsoft.Json')
-rw-r--r--Src/Newtonsoft.Json/Converters/CustomCreationConverter.cs11
-rw-r--r--Src/Newtonsoft.Json/Converters/HtmlColorConverter.cs11
-rw-r--r--Src/Newtonsoft.Json/JsonConverter.cs28
-rw-r--r--Src/Newtonsoft.Json/JsonSerializer.cs20
-rw-r--r--Src/Newtonsoft.Json/Linq/JValue.cs2
-rw-r--r--Src/Newtonsoft.Json/Schema/JsonSchemaGenerator.cs12
-rw-r--r--Src/Newtonsoft.Json/Serialization/DefaultContractResolver.cs4
-rw-r--r--Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs33
-rw-r--r--Src/Newtonsoft.Json/Serialization/JsonSerializerInternalWriter.cs5
9 files changed, 90 insertions, 36 deletions
diff --git a/Src/Newtonsoft.Json/Converters/CustomCreationConverter.cs b/Src/Newtonsoft.Json/Converters/CustomCreationConverter.cs
index d5f6441..e20fbfc 100644
--- a/Src/Newtonsoft.Json/Converters/CustomCreationConverter.cs
+++ b/Src/Newtonsoft.Json/Converters/CustomCreationConverter.cs
@@ -79,5 +79,16 @@ namespace Newtonsoft.Json.Converters
{
return typeof (T).IsAssignableFrom(objectType);
}
+
+ /// <summary>
+ /// Gets a value indicating whether this <see cref="JsonConverter"/> can write JSON.
+ /// </summary>
+ /// <value>
+ /// <c>true</c> if this <see cref="JsonConverter"/> can write JSON; otherwise, <c>false</c>.
+ /// </value>
+ public override bool CanWrite
+ {
+ get { return false; }
+ }
}
} \ No newline at end of file
diff --git a/Src/Newtonsoft.Json/Converters/HtmlColorConverter.cs b/Src/Newtonsoft.Json/Converters/HtmlColorConverter.cs
index fdde9c3..5ae24f8 100644
--- a/Src/Newtonsoft.Json/Converters/HtmlColorConverter.cs
+++ b/Src/Newtonsoft.Json/Converters/HtmlColorConverter.cs
@@ -68,6 +68,17 @@ namespace Newtonsoft.Json.Converters
{
throw new NotImplementedException();
}
+
+ /// <summary>
+ /// Gets a value indicating whether this <see cref="JsonConverter"/> can read JSON.
+ /// </summary>
+ /// <value>
+ /// <c>true</c> if this <see cref="JsonConverter"/> can read JSON; otherwise, <c>false</c>.
+ /// </value>
+ public override bool CanRead
+ {
+ get { return false; }
+ }
}
}
#endif \ No newline at end of file
diff --git a/Src/Newtonsoft.Json/JsonConverter.cs b/Src/Newtonsoft.Json/JsonConverter.cs
index 27f356c..493e252 100644
--- a/Src/Newtonsoft.Json/JsonConverter.cs
+++ b/Src/Newtonsoft.Json/JsonConverter.cs
@@ -27,6 +27,7 @@ using System;
using System.Collections.Generic;
using System.Text;
using Newtonsoft.Json.Utilities;
+using Newtonsoft.Json.Schema;
namespace Newtonsoft.Json
{
@@ -60,5 +61,32 @@ namespace Newtonsoft.Json
/// <c>true</c> if this instance can convert the specified object type; otherwise, <c>false</c>.
/// </returns>
public abstract bool CanConvert(Type objectType);
+
+ /// <summary>
+ /// Gets the <see cref="JsonSchema"/> of the JSON produced by the JsonConverter.
+ /// </summary>
+ /// <returns>The <see cref="JsonSchema"/> of the JSON produced by the JsonConverter.</returns>
+ public virtual JsonSchema GetSchema()
+ {
+ return null;
+ }
+
+ /// <summary>
+ /// Gets a value indicating whether this <see cref="JsonConverter"/> can read JSON.
+ /// </summary>
+ /// <value><c>true</c> if this <see cref="JsonConverter"/> can read JSON; otherwise, <c>false</c>.</value>
+ public virtual bool CanRead
+ {
+ get { return true; }
+ }
+
+ /// <summary>
+ /// Gets a value indicating whether this <see cref="JsonConverter"/> can write JSON.
+ /// </summary>
+ /// <value><c>true</c> if this <see cref="JsonConverter"/> can write JSON; otherwise, <c>false</c>.</value>
+ public virtual bool CanWrite
+ {
+ get { return true; }
+ }
}
} \ No newline at end of file
diff --git a/Src/Newtonsoft.Json/JsonSerializer.cs b/Src/Newtonsoft.Json/JsonSerializer.cs
index f943579..f759597 100644
--- a/Src/Newtonsoft.Json/JsonSerializer.cs
+++ b/Src/Newtonsoft.Json/JsonSerializer.cs
@@ -432,18 +432,14 @@ namespace Newtonsoft.Json
serializerWriter.Serialize(jsonWriter, value);
}
- internal bool HasMatchingConverter(Type type, out JsonConverter matchingConverter)
+ internal JsonConverter GetMatchingConverter(Type type)
{
- if (HasMatchingConverter(_converters, type, out matchingConverter))
- return true;
-
- return false;
+ return GetMatchingConverter(_converters, type);
}
- internal static bool HasMatchingConverter(IList<JsonConverter> converters, Type objectType, out JsonConverter matchingConverter)
+ internal static JsonConverter GetMatchingConverter(IList<JsonConverter> converters, Type objectType)
{
- if (objectType == null)
- throw new ArgumentNullException("objectType");
+ ValidationUtils.ArgumentNotNull(objectType, "objectType");
if (converters != null)
{
@@ -452,15 +448,11 @@ namespace Newtonsoft.Json
JsonConverter converter = converters[i];
if (converter.CanConvert(objectType))
- {
- matchingConverter = converter;
- return true;
- }
+ return converter;
}
}
- matchingConverter = null;
- return false;
+ return null;
}
internal void OnError(ErrorEventArgs e)
diff --git a/Src/Newtonsoft.Json/Linq/JValue.cs b/Src/Newtonsoft.Json/Linq/JValue.cs
index 3d95460..8df6dd9 100644
--- a/Src/Newtonsoft.Json/Linq/JValue.cs
+++ b/Src/Newtonsoft.Json/Linq/JValue.cs
@@ -273,7 +273,7 @@ namespace Newtonsoft.Json.Linq
{
JsonConverter matchingConverter;
- if (value != null && JsonSerializer.HasMatchingConverter(converters, value.GetType(), out matchingConverter))
+ if (value != null && ((matchingConverter = JsonSerializer.GetMatchingConverter(converters, value.GetType())) != null))
matchingConverter.WriteJson(writer, value, new JsonSerializer());
else
defaultWrite(value);
diff --git a/Src/Newtonsoft.Json/Schema/JsonSchemaGenerator.cs b/Src/Newtonsoft.Json/Schema/JsonSchemaGenerator.cs
index a78c81e..805bccf 100644
--- a/Src/Newtonsoft.Json/Schema/JsonSchemaGenerator.cs
+++ b/Src/Newtonsoft.Json/Schema/JsonSchemaGenerator.cs
@@ -234,6 +234,15 @@ namespace Newtonsoft.Json.Schema
throw new Exception("Unresolved circular reference for type '{0}'. Explicitly define an Id for the type using a JsonObject/JsonArray attribute or automatically generate a type Id using the UndefinedSchemaIdHandling property.".FormatWith(CultureInfo.InvariantCulture, type));
}
+ JsonContract contract = ContractResolver.ResolveContract(type);
+ JsonConverter converter;
+ if ((converter = contract.Converter) != null || (converter = contract.InternalConverter) != null)
+ {
+ JsonSchema converterSchema = converter.GetSchema();
+ if (converterSchema != null)
+ return converterSchema;
+ }
+
Push(new TypeSchema(type, new JsonSchema()));
if (explicitId != null)
@@ -244,8 +253,7 @@ namespace Newtonsoft.Json.Schema
CurrentSchema.Title = GetTitle(type);
CurrentSchema.Description = GetDescription(type);
- JsonContract contract = ContractResolver.ResolveContract(type);
- if (contract.Converter != null || contract.InternalConverter != null)
+ if (converter != null)
{
// todo: Add GetSchema to JsonConverter and use here?
CurrentSchema.Type = JsonSchemaType.Any;
diff --git a/Src/Newtonsoft.Json/Serialization/DefaultContractResolver.cs b/Src/Newtonsoft.Json/Serialization/DefaultContractResolver.cs
index 73eb3bb..62d10f4 100644
--- a/Src/Newtonsoft.Json/Serialization/DefaultContractResolver.cs
+++ b/Src/Newtonsoft.Json/Serialization/DefaultContractResolver.cs
@@ -204,9 +204,7 @@ namespace Newtonsoft.Json.Serialization
contract.Converter = ResolveContractConverter(contract.UnderlyingType);
// then see whether object is compadible with any of the built in converters
- JsonConverter internalConverter;
- if (JsonSerializer.HasMatchingConverter(BuiltInConverters, contract.UnderlyingType, out internalConverter))
- contract.InternalConverter = internalConverter;
+ contract.InternalConverter = JsonSerializer.GetMatchingConverter(BuiltInConverters, contract.UnderlyingType);
if (ReflectionUtils.HasDefaultConstructor(contract.CreatedType, true)
|| contract.CreatedType.IsValueType)
diff --git a/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs b/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs
index f456fbb..d5e832a 100644
--- a/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs
+++ b/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs
@@ -179,24 +179,29 @@ namespace Newtonsoft.Json.Serialization
private object CreateValue(JsonReader reader, Type objectType, JsonContract contract, object existingValue, JsonConverter memberConverter)
{
- JsonConverter converter;
-
- // member attribute converter
+ JsonConverter converter = null;
if (memberConverter != null)
- return memberConverter.ReadJson(reader, objectType, GetInternalSerializer());
-
- // class attribute converter
- if (contract != null && contract.Converter != null)
- return contract.Converter.ReadJson(reader, objectType, GetInternalSerializer());
+ {
+ // member attribute converter
+ converter = memberConverter;
+ }
+ else if (contract != null)
+ {
+ JsonConverter matchingConverter;
+ if (contract.Converter != null)
+ // class attribute converter
+ converter = contract.Converter;
+ else if ((matchingConverter = Serializer.GetMatchingConverter(contract.UnderlyingType)) != null)
+ // passed in converters
+ converter = matchingConverter;
+ else if (contract.InternalConverter != null)
+ // internally specified converter
+ converter = contract.InternalConverter;
+ }
- // passed in converters
- if (objectType != null && Serializer.HasMatchingConverter(objectType, out converter))
+ if (converter != null && converter.CanRead)
return converter.ReadJson(reader, objectType, GetInternalSerializer());
- // internally specified converter
- if (contract != null && contract.InternalConverter != null)
- return contract.InternalConverter.ReadJson(reader, objectType, GetInternalSerializer());
-
if (contract is JsonLinqContract)
return CreateJToken(reader, contract);
diff --git a/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalWriter.cs b/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalWriter.cs
index 59d0517..c01b72a 100644
--- a/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalWriter.cs
+++ b/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalWriter.cs
@@ -90,10 +90,11 @@ namespace Newtonsoft.Json.Serialization
return;
}
- if (converter != null
+ if ((converter != null
|| ((converter = contract.Converter) != null)
- || Serializer.HasMatchingConverter(contract.UnderlyingType, out converter)
+ || ((converter = Serializer.GetMatchingConverter(contract.UnderlyingType)) != null)
|| ((converter = contract.InternalConverter) != null))
+ && converter.CanWrite)
{
SerializeConvertable(writer, converter, value, contract);
}