Skip to content

Commit

Permalink
Merge pull request #951 from RicoSuter/master
Browse files Browse the repository at this point in the history
Release v9.13.32
  • Loading branch information
RicoSuter authored Apr 18, 2019
2 parents b03ce1a + 3e287db commit 482ce16
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PropertyGroup>
<TargetFrameworks>netstandard1.3;netstandard2.0;net451</TargetFrameworks>
<Description>JSON Schema reader, generator and validator for .NET</Description>
<Version>9.13.31</Version>
<Version>9.13.32</Version>
<PackageTags>json schema validation generator .net</PackageTags>
<Copyright>Copyright © Rico Suter, 2018</Copyright>
<PackageLicenseUrl>https://github.com/rsuter/NJsonSchema/blob/master/LICENSE.md</PackageLicenseUrl>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PropertyGroup>
<TargetFrameworks>netstandard1.3;netstandard2.0;net451</TargetFrameworks>
<Description>JSON Schema reader, generator and validator for .NET</Description>
<Version>9.13.31</Version>
<Version>9.13.32</Version>
<PackageTags>json schema validation generator .net</PackageTags>
<Copyright>Copyright © Rico Suter, 2018</Copyright>
<PackageLicenseUrl>https://github.com/rsuter/NJsonSchema/blob/master/LICENSE.md</PackageLicenseUrl>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PropertyGroup>
<TargetFrameworks>netstandard1.3;netstandard2.0;net451</TargetFrameworks>
<Description>JSON Schema reader, generator and validator for .NET</Description>
<Version>9.13.31</Version>
<Version>9.13.32</Version>
<PackageTags>json schema validation generator .net</PackageTags>
<Copyright>Copyright © Rico Suter, 2018</Copyright>
<PackageLicenseUrl>https://github.com/rsuter/NJsonSchema/blob/master/LICENSE.md</PackageLicenseUrl>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,17 @@ public void When_property_is_renamed_then_it_does_not_land_in_extension_data()
//// Arrange
var resolver = new PropertyRenameAndIgnoreSerializerContractResolver();
resolver.RenameProperty(typeof(JsonProperty), "x-readOnly", "readOnly");
resolver.RenameProperty(typeof(JsonSchema4), "x-nullable", "nullable");

var json = "{ \"readOnly\": true }";
var json = "{ \"readOnly\": true, \"nullable\": true, \"additionalProperties\": { \"nullable\": true } }";

//// Act
var obj = JsonConvert.DeserializeObject<JsonProperty>(json, new JsonSerializerSettings { ContractResolver = resolver });
var obj = JsonSchemaSerialization.FromJson<JsonProperty>(json, resolver);

//// Assert
Assert.True(obj.IsReadOnly);
Assert.True(obj.IsNullableRaw);
Assert.True(obj.AdditionalPropertiesSchema.IsNullableRaw);
}

public class MyClass
Expand Down
2 changes: 1 addition & 1 deletion src/NJsonSchema.Yaml/NJsonSchema.Yaml.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PropertyGroup>
<TargetFrameworks>netstandard1.3;netstandard2.0;net45</TargetFrameworks>
<Description>JSON Schema reader, generator and validator for .NET</Description>
<Version>9.13.31</Version>
<Version>9.13.32</Version>
<PackageTags>json schema validation generator .net</PackageTags>
<Copyright>Copyright © Rico Suter, 2018</Copyright>
<PackageLicenseUrl>https://github.com/rsuter/NJsonSchema/blob/master/LICENSE.md</PackageLicenseUrl>
Expand Down
70 changes: 54 additions & 16 deletions src/NJsonSchema/Infrastructure/JsonSchemaSerialization.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,23 @@ public class JsonSchemaSerialization
[ThreadStatic]
private static bool _isWriting;

[ThreadStatic]
private static JsonSerializerSettings _currentSerializerSettings;

/// <summary>Gets or sets the current schema type.</summary>
public static SchemaType CurrentSchemaType
{
get => _currentSchemaType;
private set => _currentSchemaType = value;
}

/// <summary>Gets the current serializer settings.</summary>
public static JsonSerializerSettings CurrentSerializerSettings
{
get => _currentSerializerSettings;
private set => _currentSerializerSettings = value;
}

/// <summary>Gets or sets a value indicating whether the object is currently converted to JSON.</summary>
public static bool IsWriting
{
Expand All @@ -54,19 +64,34 @@ public static string ToJson(object obj, SchemaType schemaType, IContractResolver
/// <param name="schemaType">The schema type.</param>
/// <param name="contractResolver">The contract resolver.</param>
/// <param name="formatting">The formatting.</param>
/// <returns></returns>
/// <returns>The JSON.</returns>
public static string ToJson(object obj, SchemaType schemaType, IContractResolver contractResolver, Formatting formatting)
{
IsWriting = false;
CurrentSchemaType = schemaType;

JsonSchemaReferenceUtilities.UpdateSchemaReferencePaths(obj, false, contractResolver);
var json = JsonConvert.SerializeObject(obj, formatting, new JsonSerializerSettings
var json = FromJson(obj, contractResolver, formatting);
return JsonSchemaReferenceUtilities.ConvertPropertyReferences(json);
}

/// <summary>Serializes an object to a JSON string.</summary>
/// <param name="obj">The object to serialize.</param>
/// <param name="contractResolver">The contract resolver.</param>
/// <param name="formatting">The formatting.</param>
/// <returns>The JSON.</returns>
public static string FromJson(object obj, IContractResolver contractResolver, Formatting formatting)
{
IsWriting = false;
CurrentSerializerSettings = new JsonSerializerSettings
{
ContractResolver = contractResolver
});
};

return JsonSchemaReferenceUtilities.ConvertPropertyReferences(json);
var json = JsonConvert.SerializeObject(obj, formatting, CurrentSerializerSettings);
CurrentSerializerSettings = null;

return json;
}

/// <summary>Deserializes JSON data to a schema with reference handling.</summary>
Expand All @@ -79,20 +104,9 @@ public static string ToJson(object obj, SchemaType schemaType, IContractResolver
public static async Task<T> FromJsonAsync<T>(string json, SchemaType schemaType, string documentPath,
Func<T, JsonReferenceResolver> referenceResolverFactory, IContractResolver contractResolver)
{
IsWriting = true;
CurrentSchemaType = schemaType;

json = JsonSchemaReferenceUtilities.ConvertJsonReferences(json);
var settings = new JsonSerializerSettings
{
ContractResolver = contractResolver,
MetadataPropertyHandling = MetadataPropertyHandling.Ignore,
ConstructorHandling = ConstructorHandling.Default,
ReferenceLoopHandling = ReferenceLoopHandling.Serialize,
PreserveReferencesHandling = PreserveReferencesHandling.Objects
};

var schema = JsonConvert.DeserializeObject<T>(json, settings);
var schema = FromJson<T>(json, contractResolver);
if (schema is IDocumentPathProvider documentPathProvider)
documentPathProvider.DocumentPath = documentPath;

Expand All @@ -106,5 +120,29 @@ public static async Task<T> FromJsonAsync<T>(string json, SchemaType schemaType,
await JsonSchemaReferenceUtilities.UpdateSchemaReferencesAsync(schema, referenceResolver, contractResolver).ConfigureAwait(false);
return schema;
}

/// <summary>Deserializes JSON data with the given contract resolver.</summary>
/// <param name="json">The JSON data.</param>
/// <param name="contractResolver">The contract resolver.</param>
/// <returns>The deserialized schema.</returns>
public static T FromJson<T>(string json, IContractResolver contractResolver)
{
IsWriting = true;
CurrentSerializerSettings = new JsonSerializerSettings
{
ContractResolver = contractResolver,
MetadataPropertyHandling = MetadataPropertyHandling.Ignore,
ConstructorHandling = ConstructorHandling.Default,
ReferenceLoopHandling = ReferenceLoopHandling.Serialize,
PreserveReferencesHandling = PreserveReferencesHandling.Objects
};

json = JsonSchemaReferenceUtilities.ConvertJsonReferences(json);

var obj = JsonConvert.DeserializeObject<T>(json, CurrentSerializerSettings);
CurrentSerializerSettings = null;

return obj;
}
}
}
8 changes: 4 additions & 4 deletions src/NJsonSchema/JsonSchema4.Serialization.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ internal object AdditionalItemsRaw
else if (value != null && (value.Equals("true") || value.Equals("false")))
AllowAdditionalItems = value.Equals("true");
else if (value != null)
AdditionalItemsSchema = FromJsonWithoutReferenceHandling(value.ToString());
AdditionalItemsSchema = FromJsonWithCurrentSettings(value.ToString());
}
}

Expand All @@ -189,7 +189,7 @@ internal object AdditionalPropertiesRaw
else if (value != null && (value.Equals("true") || value.Equals("false")))
AllowAdditionalProperties = value.Equals("true");
else if (value != null)
AdditionalPropertiesSchema = FromJsonWithoutReferenceHandling(value.ToString());
AdditionalPropertiesSchema = FromJsonWithCurrentSettings(value.ToString());
}
}

Expand All @@ -207,9 +207,9 @@ internal object ItemsRaw
set
{
if (value is JArray)
Items = new ObservableCollection<JsonSchema4>(((JArray)value).Select(t => FromJsonWithoutReferenceHandling(t.ToString())));
Items = new ObservableCollection<JsonSchema4>(((JArray)value).Select(t => FromJsonWithCurrentSettings(t.ToString())));
else if (value != null)
Item = FromJsonWithoutReferenceHandling(value.ToString());
Item = FromJsonWithCurrentSettings(value.ToString());
}
}

Expand Down
11 changes: 2 additions & 9 deletions src/NJsonSchema/JsonSchema4.cs
Original file line number Diff line number Diff line change
Expand Up @@ -191,16 +191,9 @@ public static async Task<JsonSchema4> FromJsonAsync(string data, string document
return await JsonSchemaSerialization.FromJsonAsync(data, SerializationSchemaType, documentPath, referenceResolverFactory, ContractResolver.Value).ConfigureAwait(false);
}

internal static JsonSchema4 FromJsonWithoutReferenceHandling(string data)
internal static JsonSchema4 FromJsonWithCurrentSettings(string data)
{
var schema = JsonConvert.DeserializeObject<JsonSchema4>(data, new JsonSerializerSettings
{
ConstructorHandling = ConstructorHandling.Default,
ReferenceLoopHandling = ReferenceLoopHandling.Serialize,
PreserveReferencesHandling = PreserveReferencesHandling.Objects
});

return schema;
return JsonConvert.DeserializeObject<JsonSchema4>(data, JsonSchemaSerialization.CurrentSerializerSettings);
}

/// <summary>Gets a value indicating whether the schema is binary (file or binary format).</summary>
Expand Down
2 changes: 1 addition & 1 deletion src/NJsonSchema/NJsonSchema.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PropertyGroup>
<TargetFrameworks>netstandard1.0;netstandard2.0;net40;net45</TargetFrameworks>
<Description>JSON Schema reader, generator and validator for .NET</Description>
<Version>9.13.31</Version>
<Version>9.13.32</Version>
<PackageTags>json schema validation generator .net</PackageTags>
<Copyright>Copyright © Rico Suter, 2018</Copyright>
<PackageLicenseUrl>https://github.com/rsuter/NJsonSchema/blob/master/LICENSE.md</PackageLicenseUrl>
Expand Down

0 comments on commit 482ce16

Please sign in to comment.