mirror of https://github.com/Squidex/squidex.git
26 changed files with 356 additions and 172 deletions
@ -0,0 +1,101 @@ |
|||
// ==========================================================================
|
|||
// SchemaJsonSerializer.cs
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex Group
|
|||
// All rights reserved.
|
|||
// ==========================================================================
|
|||
|
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using Newtonsoft.Json; |
|||
using Newtonsoft.Json.Linq; |
|||
using Squidex.Infrastructure; |
|||
// ReSharper disable UseObjectOrCollectionInitializer
|
|||
|
|||
namespace Squidex.Core.Schemas.Json |
|||
{ |
|||
public sealed class SchemaJsonSerializer |
|||
{ |
|||
private readonly FieldRegistry fieldRegistry; |
|||
private readonly JsonSerializer serializer; |
|||
|
|||
public class FieldModel |
|||
{ |
|||
public string Name; |
|||
|
|||
public bool IsHidden; |
|||
|
|||
public bool IsDisabled; |
|||
|
|||
public FieldProperties Properties; |
|||
} |
|||
|
|||
public sealed class SchemaModel |
|||
{ |
|||
public string Name; |
|||
|
|||
public SchemaProperties Properties; |
|||
|
|||
public Dictionary<long, FieldModel> Fields; |
|||
} |
|||
|
|||
public SchemaJsonSerializer(FieldRegistry fieldRegistry, JsonSerializerSettings serializerSettings) |
|||
{ |
|||
Guard.NotNull(fieldRegistry, nameof(fieldRegistry)); |
|||
Guard.NotNull(serializerSettings, nameof(serializerSettings)); |
|||
|
|||
this.fieldRegistry = fieldRegistry; |
|||
|
|||
serializer = JsonSerializer.Create(serializerSettings); |
|||
} |
|||
|
|||
public JToken Serialize(Schema schema) |
|||
{ |
|||
var model = new SchemaModel { Name = schema.Name, Properties = schema.Properties }; |
|||
|
|||
model.Fields = |
|||
schema.Fields |
|||
.Select(x => |
|||
new KeyValuePair<long, FieldModel>(x.Key, |
|||
new FieldModel |
|||
{ |
|||
Name = x.Value.Name, |
|||
IsHidden = x.Value.IsHidden, |
|||
IsDisabled = x.Value.IsDisabled, |
|||
Properties = x.Value.RawProperties |
|||
})) |
|||
.ToDictionary(x => x.Key, x => x.Value); |
|||
|
|||
return JToken.FromObject(model, serializer); |
|||
} |
|||
|
|||
public Schema Deserialize(JToken token) |
|||
{ |
|||
var model = token.ToObject<SchemaModel>(serializer); |
|||
|
|||
var schema = Schema.Create(model.Name, model.Properties); |
|||
|
|||
foreach (var kvp in model.Fields) |
|||
{ |
|||
var fieldModel = kvp.Value; |
|||
|
|||
var field = fieldRegistry.CreateField(kvp.Key, fieldModel.Name, fieldModel.Properties); |
|||
|
|||
if (fieldModel.IsDisabled) |
|||
{ |
|||
field = field.Disable(); |
|||
} |
|||
|
|||
if (fieldModel.IsHidden) |
|||
{ |
|||
field = field.Hide(); |
|||
} |
|||
|
|||
schema = schema.AddOrUpdateField(field); |
|||
} |
|||
|
|||
return schema; |
|||
} |
|||
} |
|||
} |
|||
@ -1,51 +0,0 @@ |
|||
// ==========================================================================
|
|||
// FieldDto.cs
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex Group
|
|||
// All rights reserved.
|
|||
// ==========================================================================
|
|||
|
|||
using Squidex.Core.Schemas; |
|||
|
|||
namespace Squidex.Store.MongoDb.Schemas.Models |
|||
{ |
|||
public class FieldModel |
|||
{ |
|||
public string Name { get; set; } |
|||
|
|||
public bool IsHidden { get; set; } |
|||
|
|||
public bool IsDisabled { get; set; } |
|||
|
|||
public FieldProperties Properties { get; set; } |
|||
|
|||
public static FieldModel Create(Field field) |
|||
{ |
|||
return new FieldModel |
|||
{ |
|||
Name = field.Name, |
|||
IsHidden = field.IsHidden, |
|||
IsDisabled = field.IsDisabled, |
|||
Properties = field.RawProperties |
|||
}; |
|||
} |
|||
|
|||
public Field ToField(long id, FieldRegistry registry) |
|||
{ |
|||
var field = registry.CreateField(id, Name, Properties); |
|||
|
|||
if (IsHidden) |
|||
{ |
|||
field = field.Hide(); |
|||
} |
|||
|
|||
if (IsDisabled) |
|||
{ |
|||
field = field.Disable(); |
|||
} |
|||
|
|||
return field; |
|||
} |
|||
} |
|||
} |
|||
@ -1,60 +0,0 @@ |
|||
// ==========================================================================
|
|||
// SchemaDto.cs
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex Group
|
|||
// All rights reserved.
|
|||
// ==========================================================================
|
|||
|
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using Squidex.Core.Schemas; |
|||
using Squidex.Infrastructure; |
|||
|
|||
// ReSharper disable UseObjectOrCollectionInitializer
|
|||
// ReSharper disable InvertIf
|
|||
|
|||
namespace Squidex.Store.MongoDb.Schemas.Models |
|||
{ |
|||
public sealed class SchemaModel |
|||
{ |
|||
public string Name { get; set; } |
|||
|
|||
public Dictionary<long, FieldModel> Fields { get; set; } |
|||
|
|||
public SchemaProperties Properties { get; set; } |
|||
|
|||
public static SchemaModel Create(Schema schema) |
|||
{ |
|||
Guard.NotNull(schema, nameof(schema)); |
|||
|
|||
var dto = new SchemaModel { Properties = schema.Properties, Name = schema.Name }; |
|||
|
|||
dto.Fields = |
|||
schema.Fields.ToDictionary( |
|||
kvp => kvp.Key, |
|||
kvp => FieldModel.Create(kvp.Value)); |
|||
|
|||
return dto; |
|||
} |
|||
|
|||
public Schema ToSchema(FieldRegistry registry) |
|||
{ |
|||
Guard.NotNull(registry, nameof(registry)); |
|||
|
|||
var schema = Schema.Create(Name, Properties); |
|||
|
|||
if (Fields != null) |
|||
{ |
|||
foreach (var kvp in Fields) |
|||
{ |
|||
var field = kvp.Value; |
|||
|
|||
schema = schema.AddOrUpdateField(field.ToField(kvp.Key, registry)); |
|||
} |
|||
} |
|||
|
|||
return schema; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,68 @@ |
|||
// ==========================================================================
|
|||
// SchemaConverter.cs
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex Group
|
|||
// All rights reserved.
|
|||
// ==========================================================================
|
|||
|
|||
using System; |
|||
using System.Collections.Generic; |
|||
using Squidex.Core.Schemas; |
|||
using Squidex.Infrastructure.Reflection; |
|||
using Squidex.Read.Schemas.Repositories; |
|||
using Dtos = Squidex.Controllers.Api.Schemas.Models.Fields; |
|||
|
|||
namespace Squidex.Controllers.Api.Schemas.Models.Converters |
|||
{ |
|||
public static class SchemaConverter |
|||
{ |
|||
private static readonly Dictionary<Type, Func<Field, FieldDto>> Factories = new Dictionary<Type, Func<Field, FieldDto>> |
|||
{ |
|||
{ |
|||
typeof(NumberField), |
|||
field => |
|||
{ |
|||
var dto = new Dtos.NumberField(); |
|||
|
|||
SimpleMapper.Map(field, dto); |
|||
SimpleMapper.Map((NumberFieldProperties)field.RawProperties, dto); |
|||
|
|||
return dto; |
|||
} |
|||
}, |
|||
{ |
|||
typeof(StringField), |
|||
field => |
|||
{ |
|||
var dto = new Dtos.StringField(); |
|||
|
|||
SimpleMapper.Map(field, dto); |
|||
SimpleMapper.Map((StringFieldProperties)field.RawProperties, dto); |
|||
|
|||
return dto; |
|||
} |
|||
} |
|||
}; |
|||
|
|||
public static SchemaDetailsDto ToModel(this ISchemaEntityWithSchema entity) |
|||
{ |
|||
var dto = new SchemaDetailsDto(); |
|||
|
|||
SimpleMapper.Map(entity, dto); |
|||
SimpleMapper.Map(entity.Schema, dto); |
|||
SimpleMapper.Map(entity.Schema.Properties, dto); |
|||
|
|||
dto.Fields = new List<FieldDto>(); |
|||
|
|||
foreach (var field in entity.Schema.Fields.Values) |
|||
{ |
|||
var fieldDto = Factories[field.RawProperties.GetType()](field); |
|||
|
|||
dto.Fields.Add(fieldDto); |
|||
} |
|||
|
|||
return dto; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,52 @@ |
|||
// ==========================================================================
|
|||
// JsonSerializerTests.cs
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex Group
|
|||
// All rights reserved.
|
|||
// ==========================================================================
|
|||
|
|||
using System.Reflection; |
|||
using FluentAssertions; |
|||
using Newtonsoft.Json; |
|||
using Squidex.Core.Schemas; |
|||
using Squidex.Core.Schemas.Json; |
|||
using Squidex.Infrastructure; |
|||
using Squidex.Infrastructure.Json; |
|||
using Xunit; |
|||
|
|||
namespace Squidex.Core.Tests.Schemas.Json |
|||
{ |
|||
public class JsonSerializerTests |
|||
{ |
|||
private static readonly JsonSerializerSettings serializerSettings = new JsonSerializerSettings(); |
|||
|
|||
static JsonSerializerTests() |
|||
{ |
|||
TypeNameRegistry.Map(typeof(FieldRegistry).GetTypeInfo().Assembly); |
|||
|
|||
serializerSettings.TypeNameHandling = TypeNameHandling.Auto; |
|||
serializerSettings.SerializationBinder = new TypeNameSerializationBinder(); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Should_serialize_and_deserialize_schema() |
|||
{ |
|||
var schema = |
|||
Schema.Create("my-schema", new SchemaProperties()) |
|||
.AddOrUpdateField(new StringField(1, "field1", new StringFieldProperties { Label = "Field1", Pattern = "[0-9]{3}" })) |
|||
.AddOrUpdateField(new NumberField(2, "field2", new NumberFieldProperties { Hints = "Hints" })) |
|||
.DisableField(1) |
|||
.HideField(2); |
|||
|
|||
|
|||
var sut = new SchemaJsonSerializer(new FieldRegistry(), serializerSettings); |
|||
|
|||
var token = sut.Serialize(schema); |
|||
|
|||
var deserialized = sut.Deserialize(token); |
|||
|
|||
deserialized.ShouldBeEquivalentTo(schema); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue