mirror of https://github.com/Squidex/squidex.git
13 changed files with 345 additions and 233 deletions
@ -0,0 +1,41 @@ |
|||||
|
// ==========================================================================
|
||||
|
// SchemaConverter.cs
|
||||
|
// Squidex Headless CMS
|
||||
|
// ==========================================================================
|
||||
|
// Copyright (c) Squidex Group
|
||||
|
// All rights reserved.
|
||||
|
// ==========================================================================
|
||||
|
|
||||
|
using System; |
||||
|
using Newtonsoft.Json; |
||||
|
using Squidex.Infrastructure; |
||||
|
|
||||
|
namespace Squidex.Domain.Apps.Core.Schemas.Json |
||||
|
{ |
||||
|
public sealed class SchemaConverter : JsonConverter |
||||
|
{ |
||||
|
private readonly FieldRegistry fieldRegistry; |
||||
|
|
||||
|
public SchemaConverter(FieldRegistry fieldRegistry) |
||||
|
{ |
||||
|
Guard.NotNull(fieldRegistry, nameof(fieldRegistry)); |
||||
|
|
||||
|
this.fieldRegistry = fieldRegistry; |
||||
|
} |
||||
|
|
||||
|
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) |
||||
|
{ |
||||
|
serializer.Serialize(writer, new JsonSchemaModel((Schema)value)); |
||||
|
} |
||||
|
|
||||
|
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) |
||||
|
{ |
||||
|
return serializer.Deserialize<JsonSchemaModel>(reader).ToSchema(fieldRegistry); |
||||
|
} |
||||
|
|
||||
|
public override bool CanConvert(Type objectType) |
||||
|
{ |
||||
|
return objectType == typeof(Schema); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -1,89 +0,0 @@ |
|||||
// ==========================================================================
|
|
||||
// SchemaJsonSerializer.cs
|
|
||||
// Squidex Headless CMS
|
|
||||
// ==========================================================================
|
|
||||
// Copyright (c) Squidex Group
|
|
||||
// All rights reserved.
|
|
||||
// ==========================================================================
|
|
||||
|
|
||||
using System.Collections.Immutable; |
|
||||
using System.Linq; |
|
||||
using Newtonsoft.Json; |
|
||||
using Newtonsoft.Json.Linq; |
|
||||
using Squidex.Infrastructure; |
|
||||
|
|
||||
namespace Squidex.Domain.Apps.Core.Schemas.Json |
|
||||
{ |
|
||||
public sealed class SchemaJsonSerializer |
|
||||
{ |
|
||||
private readonly FieldRegistry fieldRegistry; |
|
||||
private readonly JsonSerializer serializer; |
|
||||
|
|
||||
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 JsonSchemaModel { Name = schema.Name, IsPublished = schema.IsPublished, Properties = schema.Properties }; |
|
||||
|
|
||||
model.Fields = |
|
||||
schema.Fields.Select(x => |
|
||||
new JsonFieldModel |
|
||||
{ |
|
||||
Id = x.Id, |
|
||||
Name = x.Name, |
|
||||
IsHidden = x.IsHidden, |
|
||||
IsLocked = x.IsLocked, |
|
||||
IsDisabled = x.IsDisabled, |
|
||||
Partitioning = x.Partitioning.Key, |
|
||||
Properties = x.RawProperties |
|
||||
}).ToList(); |
|
||||
|
|
||||
return JToken.FromObject(model, serializer); |
|
||||
} |
|
||||
|
|
||||
public Schema Deserialize(JToken token) |
|
||||
{ |
|
||||
var model = token.ToObject<JsonSchemaModel>(serializer); |
|
||||
|
|
||||
var fields = |
|
||||
model.Fields.Select(fieldModel => |
|
||||
{ |
|
||||
var parititonKey = new Partitioning(fieldModel.Partitioning); |
|
||||
|
|
||||
var field = fieldRegistry.CreateField(fieldModel.Id, fieldModel.Name, parititonKey, fieldModel.Properties); |
|
||||
|
|
||||
if (fieldModel.IsDisabled) |
|
||||
{ |
|
||||
field = field.Disable(); |
|
||||
} |
|
||||
|
|
||||
if (fieldModel.IsLocked) |
|
||||
{ |
|
||||
field = field.Lock(); |
|
||||
} |
|
||||
|
|
||||
if (fieldModel.IsHidden) |
|
||||
{ |
|
||||
field = field.Hide(); |
|
||||
} |
|
||||
|
|
||||
return field; |
|
||||
}).ToImmutableList(); |
|
||||
|
|
||||
var schema = |
|
||||
new Schema( |
|
||||
model.Name, |
|
||||
model.IsPublished, model.Properties, fields); |
|
||||
|
|
||||
return schema; |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
@ -0,0 +1,72 @@ |
|||||
|
// ==========================================================================
|
||||
|
// Extensions.cs
|
||||
|
// Squidex Headless CMS
|
||||
|
// ==========================================================================
|
||||
|
// Copyright (c) Squidex Group
|
||||
|
// All rights reserved.
|
||||
|
// ==========================================================================
|
||||
|
|
||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
using System.Linq; |
||||
|
using System.Text; |
||||
|
using MongoDB.Bson; |
||||
|
using Newtonsoft.Json.Linq; |
||||
|
using Squidex.Domain.Apps.Core.Contents; |
||||
|
using Squidex.Domain.Apps.Core.Schemas; |
||||
|
using Squidex.Infrastructure.MongoDb; |
||||
|
|
||||
|
namespace Squidex.Domain.Apps.Read.MongoDb.Contents |
||||
|
{ |
||||
|
public static class Extensions |
||||
|
{ |
||||
|
private const int MaxLength = 1024 * 1024; |
||||
|
|
||||
|
public static BsonDocument ToBsonDocument(this IdContentData data) |
||||
|
{ |
||||
|
return (BsonDocument)JToken.FromObject(data).ToBson(); |
||||
|
} |
||||
|
|
||||
|
public static List<Guid> ToReferencedIds(this IdContentData data, Schema schema) |
||||
|
{ |
||||
|
return data.GetReferencedIds(schema).ToList(); |
||||
|
} |
||||
|
|
||||
|
public static NamedContentData ToData(this BsonDocument document, Schema schema, List<Guid> deletedIds) |
||||
|
{ |
||||
|
return document |
||||
|
.ToJson() |
||||
|
.ToObject<IdContentData>() |
||||
|
.ToCleanedReferences(schema, new HashSet<Guid>(deletedIds)) |
||||
|
.ToNameModel(schema, true); |
||||
|
} |
||||
|
|
||||
|
public static string ToFullText<T>(this ContentData<T> data) |
||||
|
{ |
||||
|
var stringBuilder = new StringBuilder(); |
||||
|
|
||||
|
foreach (var text in data.Values.SelectMany(x => x.Values).Where(x => x != null).OfType<JValue>()) |
||||
|
{ |
||||
|
if (text.Type == JTokenType.String) |
||||
|
{ |
||||
|
var value = text.ToString(); |
||||
|
|
||||
|
if (value.Length < 1000) |
||||
|
{ |
||||
|
stringBuilder.Append(" "); |
||||
|
stringBuilder.Append(text); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
var result = stringBuilder.ToString(); |
||||
|
|
||||
|
if (result.Length > MaxLength) |
||||
|
{ |
||||
|
result = result.Substring(MaxLength); |
||||
|
} |
||||
|
|
||||
|
return result; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,109 @@ |
|||||
|
// ==========================================================================
|
||||
|
// BsonConverter.cs
|
||||
|
// Squidex Headless CMS
|
||||
|
// ==========================================================================
|
||||
|
// Copyright (c) Squidex Group
|
||||
|
// All rights reserved.
|
||||
|
// ==========================================================================
|
||||
|
|
||||
|
using System; |
||||
|
using MongoDB.Bson; |
||||
|
using Newtonsoft.Json.Linq; |
||||
|
|
||||
|
namespace Squidex.Infrastructure.MongoDb |
||||
|
{ |
||||
|
public static class BsonConverter |
||||
|
{ |
||||
|
public static BsonDocument ToBson(this JObject source) |
||||
|
{ |
||||
|
var result = new BsonDocument(); |
||||
|
|
||||
|
foreach (var property in source) |
||||
|
{ |
||||
|
result.Add(property.Key, property.Value.ToBson()); |
||||
|
} |
||||
|
|
||||
|
return result; |
||||
|
} |
||||
|
|
||||
|
public static JObject ToJson(this BsonDocument source) |
||||
|
{ |
||||
|
var result = new JObject(); |
||||
|
|
||||
|
foreach (var property in source) |
||||
|
{ |
||||
|
result.Add(property.Name, property.Value.ToJson()); |
||||
|
} |
||||
|
|
||||
|
return result; |
||||
|
} |
||||
|
|
||||
|
public static BsonArray ToBson(this JArray source) |
||||
|
{ |
||||
|
var result = new BsonArray(); |
||||
|
|
||||
|
foreach (var item in source) |
||||
|
{ |
||||
|
result.Add(item.ToBson()); |
||||
|
} |
||||
|
|
||||
|
return result; |
||||
|
} |
||||
|
|
||||
|
public static JArray ToJson(this BsonArray source) |
||||
|
{ |
||||
|
var result = new JArray(); |
||||
|
|
||||
|
foreach (var item in source) |
||||
|
{ |
||||
|
result.Add(item.ToJson()); |
||||
|
} |
||||
|
|
||||
|
return result; |
||||
|
} |
||||
|
|
||||
|
public static BsonValue ToBson(this JToken source) |
||||
|
{ |
||||
|
switch (source) |
||||
|
{ |
||||
|
case JObject jObject: |
||||
|
return jObject.ToBson(); |
||||
|
case JArray jArray: |
||||
|
return jArray.ToBson(); |
||||
|
case JValue jValue: |
||||
|
return BsonValue.Create(jValue.Value); |
||||
|
} |
||||
|
|
||||
|
throw new NotSupportedException($"Cannot convert {source.GetType()} to Bson."); |
||||
|
} |
||||
|
|
||||
|
public static JToken ToJson(this BsonValue source) |
||||
|
{ |
||||
|
switch (source.BsonType) |
||||
|
{ |
||||
|
case BsonType.Document: |
||||
|
return source.AsBsonDocument.ToJson(); |
||||
|
case BsonType.Array: |
||||
|
return source.AsBsonArray.ToJson(); |
||||
|
case BsonType.Double: |
||||
|
return new JValue(source.AsDouble); |
||||
|
case BsonType.String: |
||||
|
return new JValue(source.AsString); |
||||
|
case BsonType.Boolean: |
||||
|
return new JValue(source.AsBoolean); |
||||
|
case BsonType.DateTime: |
||||
|
return new JValue(source.ToUniversalTime()); |
||||
|
case BsonType.Int32: |
||||
|
return new JValue(source.AsInt32); |
||||
|
case BsonType.Int64: |
||||
|
return new JValue(source.AsInt64); |
||||
|
case BsonType.Decimal128: |
||||
|
return new JValue(source.AsDecimal); |
||||
|
case BsonType.Null: |
||||
|
return JValue.CreateNull(); |
||||
|
} |
||||
|
|
||||
|
throw new NotSupportedException($"Cannot convert {source.GetType()} to Json."); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue