-Change BSON writer to write new binary subtype

-Fix BSON reader to read the old binary subtype correctly
-Added JsonNet35BinaryCompadibility to BSON reader for incorrectly written BSON binary values
-Changed JSON schema to use required flags instead of optional as defined in JSON schema spec 3.0
-Changed JSON schema to use divisibleBy instead of maxDecimals as defined in JSON schema spec 3.0
-Fixed detection of serialization specified flag properties
-Updated to latest version of NuGet
This commit is contained in:
JamesNK
2010-12-19 00:29:41 +00:00
parent 2e7b8e5279
commit 5ccd826951
20 changed files with 194 additions and 80 deletions
@@ -23,11 +23,15 @@
// OTHER DEALINGS IN THE SOFTWARE.
#endregion
using System;
namespace Newtonsoft.Json.Bson
{
internal enum BsonBinaryType : byte
{
Binary = 0x00,
Function = 0x01,
[Obsolete("This type has been deprecated in the BSON specification. Use Binary instead.")]
Data = 0x02,
Uuid = 0x03,
Md5 = 0x05,
+1 -1
View File
@@ -134,7 +134,7 @@ namespace Newtonsoft.Json.Bson
byte[] data = (byte[])value.Value;
_writer.Write(data.Length);
_writer.Write((byte)BsonBinaryType.Data);
_writer.Write((byte)BsonBinaryType.Binary);
_writer.Write(data);
}
break;
+22 -2
View File
@@ -55,6 +55,7 @@ namespace Newtonsoft.Json.Bson
private ContainerContext _currentContext;
private bool _readRootValueAsArray;
private bool _jsonNet35BinaryCompadibility;
private DateTimeKind _dateTimeKindHandling;
private enum BsonReaderState
@@ -82,6 +83,18 @@ namespace Newtonsoft.Json.Bson
}
}
/// <summary>
/// Gets or sets a value indicating whether binary data reading should compatible with incorrect Json.NET 3.5 written binary.
/// </summary>
/// <value>
/// <c>true</c> if binary data reading will be compatible with incorrect Json.NET 3.5 written binary; otherwise, <c>false</c>.
/// </value>
public bool JsonNet35BinaryCompadibility
{
get { return _jsonNet35BinaryCompadibility; }
set { _jsonNet35BinaryCompadibility = value; }
}
/// <summary>
/// Gets or sets a value indicating whether the root object will be read as a JSON array.
/// </summary>
@@ -526,8 +539,15 @@ namespace Newtonsoft.Json.Bson
{
int dataLength = ReadInt32();
// BsonBinaryType not used
ReadByte();
BsonBinaryType binaryType = (BsonBinaryType)ReadByte();
#pragma warning disable 612,618
// the old binary type has the data length repeated in the data for some reason
if (binaryType == BsonBinaryType.Data && !_jsonNet35BinaryCompadibility)
{
dataLength = ReadInt32();
}
#pragma warning restore 612,618
return ReadBytes(dataLength);
}
+11 -4
View File
@@ -77,7 +77,7 @@ namespace Newtonsoft.Json
private IEnumerable<string> GetRequiredProperties(JsonSchemaModel schema)
{
return schema.Properties.Where(p => !p.Value.Optional).Select(p => p.Key);
return schema.Properties.Where(p => p.Value.Required).Select(p => p.Key);
}
}
@@ -448,7 +448,7 @@ namespace Newtonsoft.Json
requiredProperties.Where(kv => !kv.Value).Select(kv => kv.Key).ToList();
if (unmatchedRequiredProperties.Count > 0)
RaiseError("Non-optional properties are missing from object: {0}.".FormatWith(CultureInfo.InvariantCulture, string.Join(", ", unmatchedRequiredProperties.ToArray())), schema);
RaiseError("Required properties are missing from object: {0}.".FormatWith(CultureInfo.InvariantCulture, string.Join(", ", unmatchedRequiredProperties.ToArray())), schema);
}
}
@@ -564,8 +564,15 @@ namespace Newtonsoft.Json
if (schema.Minimum != null && value < schema.Minimum)
RaiseError("Float {0} is less than minimum value of {1}.".FormatWith(CultureInfo.InvariantCulture, JsonConvert.ToString(value), schema.Minimum), schema);
if (schema.MaximumDecimals != null && MathUtils.GetDecimalPlaces(value) > schema.MaximumDecimals)
RaiseError("Float {0} exceeds the maximum allowed number decimal places of {1}.".FormatWith(CultureInfo.InvariantCulture, JsonConvert.ToString(value), schema.MaximumDecimals), schema);
if (schema.DivisibleBy != null && !IsZero(value % schema.DivisibleBy.Value))
RaiseError("Float {0} is not evenly divisible by {1}.".FormatWith(CultureInfo.InvariantCulture, JsonConvert.ToString(value), schema.DivisibleBy), schema);
}
private static bool IsZero(double value)
{
double epsilon = 2.2204460492503131e-016;
return Math.Abs(value) < 10.0 * epsilon;
}
private void ValidatePropertyName(JsonSchemaModel schema)
+3 -3
View File
@@ -49,9 +49,9 @@ namespace Newtonsoft.Json.Schema
/// </summary>
public string Title { get; set; }
/// <summary>
/// Gets or sets whether the object is optional.
/// Gets or sets whether the object is required.
/// </summary>
public bool? Optional { get; set; }
public bool? Required { get; set; }
/// <summary>
/// Gets or sets whether the object is read only.
/// </summary>
@@ -92,7 +92,7 @@ namespace Newtonsoft.Json.Schema
/// Gets or sets the maximum decimals.
/// </summary>
/// <value>The maximum decimals.</value>
public int? MaximumDecimals { get; set; }
public double? DivisibleBy { get; set; }
/// <summary>
/// Gets or sets the minimum.
/// </summary>
@@ -146,8 +146,8 @@ namespace Newtonsoft.Json.Schema
case JsonSchemaConstants.AdditionalPropertiesPropertyName:
ProcessAdditionalProperties();
break;
case JsonSchemaConstants.OptionalPropertyName:
CurrentSchema.Optional = (bool)_reader.Value;
case JsonSchemaConstants.RequiredPropertyName:
CurrentSchema.Required = (bool)_reader.Value;
break;
case JsonSchemaConstants.RequiresPropertyName:
CurrentSchema.Requires = (string) _reader.Value;
@@ -173,8 +173,8 @@ namespace Newtonsoft.Json.Schema
case JsonSchemaConstants.MinimumItemsPropertyName:
CurrentSchema.MinimumItems = Convert.ToInt32(_reader.Value, CultureInfo.InvariantCulture);
break;
case JsonSchemaConstants.MaximumDecimalsPropertyName:
CurrentSchema.MaximumDecimals = Convert.ToInt32(_reader.Value, CultureInfo.InvariantCulture);
case JsonSchemaConstants.DivisibleByPropertyName:
CurrentSchema.DivisibleBy = Convert.ToDouble(_reader.Value, CultureInfo.InvariantCulture);
break;
case JsonSchemaConstants.DisallowPropertyName:
CurrentSchema.Disallow = ProcessType();
@@ -35,7 +35,7 @@ namespace Newtonsoft.Json.Schema
public const string TypePropertyName = "type";
public const string PropertiesPropertyName = "properties";
public const string ItemsPropertyName = "items";
public const string OptionalPropertyName = "optional";
public const string RequiredPropertyName = "required";
public const string AdditionalPropertiesPropertyName = "additionalProperties";
public const string RequiresPropertyName = "requires";
public const string IdentityPropertyName = "identity";
@@ -54,7 +54,7 @@ namespace Newtonsoft.Json.Schema
public const string FormatPropertyName = "format";
public const string DefaultPropertyName = "default";
public const string TransientPropertyName = "transient";
public const string MaximumDecimalsPropertyName = "maxDecimal";
public const string DivisibleByPropertyName = "divisibleBy";
public const string HiddenPropertyName = "hidden";
public const string DisallowPropertyName = "disallow";
public const string ExtendsPropertyName = "extends";
@@ -205,7 +205,7 @@ namespace Newtonsoft.Json.Schema
}
}
private JsonSchema GenerateInternal(Type type, Required valueRequired, bool optional)
private JsonSchema GenerateInternal(Type type, Required valueRequired, bool required)
{
ValidationUtils.ArgumentNotNull(type, "type");
@@ -221,8 +221,8 @@ namespace Newtonsoft.Json.Schema
// change resolved schema to allow nulls. hacky but what are ya gonna do?
if (valueRequired != Required.Always && !HasFlag(resolvedSchema.Type, JsonSchemaType.Null))
resolvedSchema.Type |= JsonSchemaType.Null;
if (optional && resolvedSchema.Optional != true)
resolvedSchema.Optional = true;
if (required && resolvedSchema.Required != true)
resolvedSchema.Required = true;
return resolvedSchema;
}
@@ -248,8 +248,8 @@ namespace Newtonsoft.Json.Schema
if (explicitId != null)
CurrentSchema.Id = explicitId;
if (optional)
CurrentSchema.Optional = true;
if (required)
CurrentSchema.Required = true;
CurrentSchema.Title = GetTitle(type);
CurrentSchema.Description = GetDescription(type);
@@ -364,7 +364,7 @@ namespace Newtonsoft.Json.Schema
property.ShouldSerialize != null ||
property.GetIsSpecified != null;
JsonSchema propertySchema = GenerateInternal(property.PropertyType, property.Required, optional);
JsonSchema propertySchema = GenerateInternal(property.PropertyType, property.Required, !optional);
if (property.DefaultValue != null)
propertySchema.Default = JToken.FromObject(property.DefaultValue);
@@ -33,11 +33,11 @@ namespace Newtonsoft.Json.Schema
{
internal class JsonSchemaModel
{
public bool Optional { get; set; }
public bool Required { get; set; }
public JsonSchemaType Type { get; set; }
public int? MinimumLength { get; set; }
public int? MaximumLength { get; set; }
public int? MaximumDecimals { get; set; }
public double? DivisibleBy { get; set; }
public double? Minimum { get; set; }
public double? Maximum { get; set; }
public int? MinimumItems { get; set; }
@@ -54,7 +54,7 @@ namespace Newtonsoft.Json.Schema
{
Type = JsonSchemaType.Any;
AllowAdditionalProperties = true;
Optional = true;
Required = false;
}
public static JsonSchemaModel Create(IList<JsonSchema> schemata)
@@ -71,12 +71,16 @@ namespace Newtonsoft.Json.Schema
private static void Combine(JsonSchemaModel model, JsonSchema schema)
{
model.Optional = model.Optional && (schema.Optional ?? false);
// Version 3 of the Draft JSON Schema has the default value of Not Required
model.Required = model.Required || (schema.Required ?? false);
model.Type = model.Type & (schema.Type ?? JsonSchemaType.Any);
model.MinimumLength = MathUtils.Max(model.MinimumLength, schema.MinimumLength);
model.MaximumLength = MathUtils.Min(model.MaximumLength, schema.MaximumLength);
model.MaximumDecimals = MathUtils.Min(model.MaximumDecimals, schema.MaximumDecimals);
// not sure what is the best way to combine divisibleBy
model.DivisibleBy = MathUtils.Max(model.DivisibleBy, schema.DivisibleBy);
model.Minimum = MathUtils.Max(model.Minimum, schema.Minimum);
model.Maximum = MathUtils.Max(model.Maximum, schema.Maximum);
model.MinimumItems = MathUtils.Max(model.MinimumItems, schema.MinimumItems);
@@ -70,7 +70,7 @@ namespace Newtonsoft.Json.Schema
WritePropertyIfNotNull(_writer, JsonSchemaConstants.IdPropertyName, schema.Id);
WritePropertyIfNotNull(_writer, JsonSchemaConstants.TitlePropertyName, schema.Title);
WritePropertyIfNotNull(_writer, JsonSchemaConstants.DescriptionPropertyName, schema.Description);
WritePropertyIfNotNull(_writer, JsonSchemaConstants.OptionalPropertyName, schema.Optional);
WritePropertyIfNotNull(_writer, JsonSchemaConstants.RequiredPropertyName, schema.Required);
WritePropertyIfNotNull(_writer, JsonSchemaConstants.ReadOnlyPropertyName, schema.ReadOnly);
WritePropertyIfNotNull(_writer, JsonSchemaConstants.HiddenPropertyName, schema.Hidden);
WritePropertyIfNotNull(_writer, JsonSchemaConstants.TransientPropertyName, schema.Transient);
@@ -107,7 +107,7 @@ namespace Newtonsoft.Json.Schema
WritePropertyIfNotNull(_writer, JsonSchemaConstants.MaximumLengthPropertyName, schema.MaximumLength);
WritePropertyIfNotNull(_writer, JsonSchemaConstants.MinimumItemsPropertyName, schema.MinimumItems);
WritePropertyIfNotNull(_writer, JsonSchemaConstants.MaximumItemsPropertyName, schema.MaximumItems);
WritePropertyIfNotNull(_writer, JsonSchemaConstants.MaximumDecimalsPropertyName, schema.MaximumDecimals);
WritePropertyIfNotNull(_writer, JsonSchemaConstants.DivisibleByPropertyName, schema.DivisibleBy);
WritePropertyIfNotNull(_writer, JsonSchemaConstants.FormatPropertyName, schema.Format);
WritePropertyIfNotNull(_writer, JsonSchemaConstants.PatternPropertyName, schema.Pattern);
if (schema.Enum != null)
@@ -743,8 +743,8 @@ namespace Newtonsoft.Json.Serialization
specifiedMember = member.DeclaringType.GetField(member.Name + JsonTypeReflector.SpecifiedPostfix);
if (specifiedMember == null || ReflectionUtils.GetMemberUnderlyingType(specifiedMember) != typeof(bool)
|| (ReflectionUtils.GetAttribute<XmlIgnoreAttribute>(specifiedMember) != null
&& ReflectionUtils.GetAttribute<JsonIgnoreAttribute>(specifiedMember) != null))
|| (ReflectionUtils.GetAttribute<XmlIgnoreAttribute>(specifiedMember) == null
&& ReflectionUtils.GetAttribute<JsonIgnoreAttribute>(specifiedMember) == null))
{
return;
}