diff --git a/Src/Newtonsoft.Json.Tests/Linq/JValueTests.cs b/Src/Newtonsoft.Json.Tests/Linq/JValueTests.cs index 8780cbc0..d760fc18 100644 --- a/Src/Newtonsoft.Json.Tests/Linq/JValueTests.cs +++ b/Src/Newtonsoft.Json.Tests/Linq/JValueTests.cs @@ -682,5 +682,40 @@ namespace Newtonsoft.Json.Tests.Linq Assert.AreEqual(expectedDate2, date2.Value); } #endif + + public class ReadOnlyStringConverter : JsonConverter + { + public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) + { + throw new NotSupportedException(); + } + + public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) + { + return reader.Value + "!"; + } + + public override bool CanConvert(Type objectType) + { + return objectType == typeof(string); + } + + public override bool CanWrite + { + get { return false; } + } + } + + [Test] + public void ReadOnlyConverterTest() + { + JObject o = new JObject(new JProperty("name", "Hello World")); + + string json = o.ToString(Formatting.Indented, new ReadOnlyStringConverter()); + + Assert.AreEqual(@"{ + ""name"": ""Hello World"" +}", json); + } } } \ No newline at end of file diff --git a/Src/Newtonsoft.Json.Tests/Properties/AssemblyInfo.cs b/Src/Newtonsoft.Json.Tests/Properties/AssemblyInfo.cs index 41c9a55e..fa1ee484 100644 --- a/Src/Newtonsoft.Json.Tests/Properties/AssemblyInfo.cs +++ b/Src/Newtonsoft.Json.Tests/Properties/AssemblyInfo.cs @@ -75,4 +75,4 @@ using System.Security; // by using the '*' as shown below: [assembly: AssemblyVersion("4.5.0.0")] -[assembly: AssemblyFileVersion("5.0.8.16620")] +[assembly: AssemblyFileVersion("5.0.8.16801")] diff --git a/Src/Newtonsoft.Json.Tests/Schema/JsonSchemaGeneratorTests.cs b/Src/Newtonsoft.Json.Tests/Schema/JsonSchemaGeneratorTests.cs index aa211f76..c1e6c385 100644 --- a/Src/Newtonsoft.Json.Tests/Schema/JsonSchemaGeneratorTests.cs +++ b/Src/Newtonsoft.Json.Tests/Schema/JsonSchemaGeneratorTests.cs @@ -758,6 +758,45 @@ namespace Newtonsoft.Json.Tests.Schema ] } } +}", json); + } + + [JsonConverter(typeof(StringEnumConverter))] + public enum SortTypeFlagAsString + { + No = 0, + Asc = 1, + Desc = -1 + } + + public class Y + { + public SortTypeFlagAsString y; + } + + [Test] + public void GenerateSchemaWithStringEnum() + { + JsonSchemaGenerator jsonSchemaGenerator = new JsonSchemaGenerator(); + JsonSchema schema = jsonSchemaGenerator.Generate(typeof(Y)); + + string json = schema.ToString(); + + // NOTE: This fails because the enum is serialized as an integer and not a string. + // NOTE: There should exist a way to serialize the enum as lowercase strings. + Assert.AreEqual(@"{ + ""type"": ""object"", + ""properties"": { + ""y"": { + ""required"": true, + ""type"": ""string"", + ""enum"": [ + ""no"", + ""asc"", + ""desc"" + ] + } + } }", json); } } diff --git a/Src/Newtonsoft.Json/Converters/StringEnumConverter.cs b/Src/Newtonsoft.Json/Converters/StringEnumConverter.cs index 6499ce70..93661d19 100644 --- a/Src/Newtonsoft.Json/Converters/StringEnumConverter.cs +++ b/Src/Newtonsoft.Json/Converters/StringEnumConverter.cs @@ -28,6 +28,7 @@ using System.Collections.Generic; using System.Globalization; using System.Reflection; using System.Runtime.Serialization; +using Newtonsoft.Json.Schema; using Newtonsoft.Json.Utilities; #if NET20 using Newtonsoft.Json.Utilities.LinqBridge; @@ -197,7 +198,7 @@ namespace Newtonsoft.Json.Converters .Select(a => a.Value) .SingleOrDefault() ?? f.Name; #else - n2 = f.Name; + n2 = f.Name; #endif string s; diff --git a/Src/Newtonsoft.Json/Linq/JValue.cs b/Src/Newtonsoft.Json/Linq/JValue.cs index ac631406..1c789b29 100644 --- a/Src/Newtonsoft.Json/Linq/JValue.cs +++ b/Src/Newtonsoft.Json/Linq/JValue.cs @@ -616,7 +616,7 @@ namespace Newtonsoft.Json.Linq if (converters != null && converters.Length > 0 && _value != null) { JsonConverter matchingConverter = JsonSerializer.GetMatchingConverter(converters, _value.GetType()); - if (matchingConverter != null) + if (matchingConverter != null && matchingConverter.CanWrite) { matchingConverter.WriteJson(writer, _value, JsonSerializer.CreateDefault()); return; diff --git a/Src/Newtonsoft.Json/Properties/AssemblyInfo.cs b/Src/Newtonsoft.Json/Properties/AssemblyInfo.cs index d40f88a0..7e22f040 100644 --- a/Src/Newtonsoft.Json/Properties/AssemblyInfo.cs +++ b/Src/Newtonsoft.Json/Properties/AssemblyInfo.cs @@ -91,5 +91,5 @@ using System.Security; // by using the '*' as shown below: [assembly: AssemblyVersion("4.5.0.0")] -[assembly: AssemblyFileVersion("5.0.8.16620")] +[assembly: AssemblyFileVersion("5.0.8.16801")] [assembly: CLSCompliant(true)] diff --git a/Src/Newtonsoft.Json/Serialization/JsonProperty.cs b/Src/Newtonsoft.Json/Serialization/JsonProperty.cs index 634e431b..58206c09 100644 --- a/Src/Newtonsoft.Json/Serialization/JsonProperty.cs +++ b/Src/Newtonsoft.Json/Serialization/JsonProperty.cs @@ -119,31 +119,31 @@ namespace Newtonsoft.Json.Serialization public JsonConverter Converter { get; set; } /// - /// Gets the member converter. + /// Gets or sets the member converter. /// /// The member converter. public JsonConverter MemberConverter { get; set; } /// - /// Gets a value indicating whether this is ignored. + /// Gets or sets a value indicating whether this is ignored. /// /// true if ignored; otherwise, false. public bool Ignored { get; set; } /// - /// Gets a value indicating whether this is readable. + /// Gets or sets a value indicating whether this is readable. /// /// true if readable; otherwise, false. public bool Readable { get; set; } /// - /// Gets a value indicating whether this is writable. + /// Gets or sets a value indicating whether this is writable. /// /// true if writable; otherwise, false. public bool Writable { get; set; } /// - /// Gets a value indicating whether this has a member attribute. + /// Gets or sets a value indicating whether this has a member attribute. /// /// true if has a member attribute; otherwise, false. public bool HasMemberAttribute { get; set; } @@ -171,7 +171,7 @@ namespace Newtonsoft.Json.Serialization } /// - /// Gets a value indicating whether this is required. + /// Gets or sets a value indicating whether this is required. /// /// A value indicating whether this is required. public Required Required @@ -181,7 +181,7 @@ namespace Newtonsoft.Json.Serialization } /// - /// Gets a value indicating whether this property preserves object references. + /// Gets or sets a value indicating whether this property preserves object references. /// /// /// true if this instance is reference; otherwise, false. @@ -189,31 +189,31 @@ namespace Newtonsoft.Json.Serialization public bool? IsReference { get; set; } /// - /// Gets the property null value handling. + /// Gets or sets the property null value handling. /// /// The null value handling. public NullValueHandling? NullValueHandling { get; set; } /// - /// Gets the property default value handling. + /// Gets or sets the property default value handling. /// /// The default value handling. public DefaultValueHandling? DefaultValueHandling { get; set; } /// - /// Gets the property reference loop handling. + /// Gets or sets the property reference loop handling. /// /// The reference loop handling. public ReferenceLoopHandling? ReferenceLoopHandling { get; set; } /// - /// Gets the property object creation handling. + /// Gets or sets the property object creation handling. /// /// The object creation handling. public ObjectCreationHandling? ObjectCreationHandling { get; set; } /// - /// Gets or sets the type name handling. + /// Gets or sets or sets the type name handling. /// /// The type name handling. public TypeNameHandling? TypeNameHandling { get; set; }