Skip to content

Commit

Permalink
Improved schema dereferencing, closes #19 and #21
Browse files Browse the repository at this point in the history
  • Loading branch information
RicoSuter committed Jul 5, 2017
1 parent 02e598a commit b6459c2
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 18 deletions.
1 change: 0 additions & 1 deletion src/VisualJsonEditor/Controls/JsonEditor.xaml
Expand Up @@ -182,7 +182,6 @@
</MultiBinding>
</Run.Text>
</Run> (<Run Text="{Binding Schema.Type, Mode=OneWay}" />)

</TextBlock>
</DataTemplate>
</controls:ExpandingGroupBox.HeaderTemplate>
Expand Down
2 changes: 1 addition & 1 deletion src/VisualJsonEditor/Controls/JsonEditor.xaml.cs
Expand Up @@ -47,7 +47,7 @@ private void OnAddArrayObject(object sender, RoutedEventArgs e)
property.Value = new ObservableCollection<JsonTokenModel>();

var list = (ObservableCollection<JsonTokenModel>)property.Value;
var schema = property.Schema.ActualPropertySchema.Item;
var schema = property.Schema.ActualPropertySchema.Item.ActualSchema;

var obj = !schema.Type.HasFlag(JsonObjectType.Object) && !schema.Type.HasFlag(JsonObjectType.Array) ?
(JsonTokenModel)new JsonValueModel { Schema = schema } : JsonObjectModel.FromSchema(schema);
Expand Down
30 changes: 18 additions & 12 deletions src/VisualJsonEditor/Models/JsonObjectModel.cs
Expand Up @@ -28,17 +28,20 @@ public class JsonObjectModel : JsonTokenModel
/// <returns>The <see cref="JsonObjectModel"/>. </returns>
public static JsonObjectModel FromSchema(JsonSchema4 schema)
{
schema = schema.ActualSchema;

var obj = new JsonObjectModel();
foreach (var property in schema.Properties)
{
if (property.Value.Type.HasFlag(JsonObjectType.Object))
var propertySchema = property.Value.ActualPropertySchema;
if (propertySchema.Type.HasFlag(JsonObjectType.Object))
{
if (property.Value.IsRequired)
obj[property.Key] = FromSchema(property.Value);
obj[property.Key] = FromSchema(propertySchema);
else
obj[property.Key] = null;
}
else if (property.Value.Type.HasFlag(JsonObjectType.Array))
else if (propertySchema.Type.HasFlag(JsonObjectType.Array))
{
if (property.Value.IsRequired)
obj[property.Key] = new ObservableCollection<JsonTokenModel>();
Expand Down Expand Up @@ -68,13 +71,15 @@ public static JsonObjectModel FromJson(string jsonData, JsonSchema4 schema)
/// <returns>The <see cref="JsonObjectModel"/>. </returns>
public static JsonObjectModel FromJson(JObject obj, JsonSchema4 schema)
{
schema = schema.ActualSchema;

var result = new JsonObjectModel();
foreach (var property in schema.Properties)
{
var propertySchema = property.Value.ActualPropertySchema;
if (propertySchema.Type.HasFlag(JsonObjectType.Array))
{
var propertyItemSchema = propertySchema.Item;
var propertyItemSchema = propertySchema.Item?.ActualSchema;
if (obj[property.Key] != null)
{
var objects = obj[property.Key].Select(o => o is JObject ?
Expand Down Expand Up @@ -113,19 +118,20 @@ public static JsonObjectModel FromJson(JObject obj, JsonSchema4 schema)

private static object GetDefaultValue(KeyValuePair<string, JsonProperty> property)
{
if (property.Value.Default != null)
return property.Value.Default;
var propertySchema = property.Value.ActualPropertySchema;
if (propertySchema.Default != null)
return propertySchema.Default;

if (property.Value.Type.HasFlag(JsonObjectType.Boolean))
if (propertySchema.Type.HasFlag(JsonObjectType.Boolean))
return false;

if (property.Value.Type.HasFlag(JsonObjectType.String) && property.Value.Format == JsonFormatStrings.DateTime)
if (propertySchema.Type.HasFlag(JsonObjectType.String) && propertySchema.Format == JsonFormatStrings.DateTime)
return new DateTime();
if (property.Value.Type.HasFlag(JsonObjectType.String) && property.Value.Format == "date") // TODO: What to do with date/time?
if (propertySchema.Type.HasFlag(JsonObjectType.String) && propertySchema.Format == "date") // TODO: What to do with date/time?
return new DateTime();
if (property.Value.Type.HasFlag(JsonObjectType.String) && property.Value.Format == "time")
if (propertySchema.Type.HasFlag(JsonObjectType.String) && propertySchema.Format == "time")
return new TimeSpan();
if (property.Value.Type.HasFlag(JsonObjectType.String))
if (propertySchema.Type.HasFlag(JsonObjectType.String))
return string.Empty;

return null;
Expand All @@ -145,7 +151,7 @@ public IEnumerable<JsonPropertyModel> Properties
if (property.Value is ObservableCollection<JsonTokenModel>)
{
foreach (var obj in (ObservableCollection<JsonTokenModel>)property.Value)
obj.Schema = propertyInfo.Value.Item;
obj.Schema = propertyInfo.Value.Item?.ActualSchema;
}
properties.Add(property);
}
Expand Down
2 changes: 1 addition & 1 deletion src/VisualJsonEditor/Properties/AssemblyInfo.cs
Expand Up @@ -18,4 +18,4 @@
ResourceDictionaryLocation.SourceAssembly
)]

[assembly: AssemblyVersion("2.3.*")]
[assembly: AssemblyVersion("2.4.*")]
5 changes: 3 additions & 2 deletions src/VisualJsonEditor/VisualJsonEditor.csproj
Expand Up @@ -59,8 +59,9 @@
<HintPath>..\packages\Newtonsoft.Json.9.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="NJsonSchema, Version=8.34.6331.29178, Culture=neutral, PublicKeyToken=c2f9c3bdfae56102, processorArchitecture=MSIL">
<HintPath>..\packages\NJsonSchema.8.34.6331.29178\lib\net45\NJsonSchema.dll</HintPath>
<Reference Include="NJsonSchema, Version=9.2.5.0, Culture=neutral, PublicKeyToken=c2f9c3bdfae56102, processorArchitecture=MSIL">
<HintPath>..\packages\NJsonSchema.9.2.5\lib\net45\NJsonSchema.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.ComponentModel.DataAnnotations" />
Expand Down
2 changes: 1 addition & 1 deletion src/VisualJsonEditor/packages.config
Expand Up @@ -6,5 +6,5 @@
<package id="MyToolkit" version="2.5.16" targetFramework="net45" />
<package id="MyToolkit.Extended" version="2.5.16" targetFramework="net45" />
<package id="Newtonsoft.Json" version="9.0.1" targetFramework="net45" />
<package id="NJsonSchema" version="8.34.6331.29178" targetFramework="net45" />
<package id="NJsonSchema" version="9.2.5" targetFramework="net45" />
</packages>

0 comments on commit b6459c2

Please sign in to comment.