mirror of https://github.com/Squidex/squidex.git
Browse Source
* #639 Create JSON schemas for all rule events * Serializers simplified. * Converters removed. * Get rid of serializer code. * Json fixes. * Tests fixed. * Tests fixed. * Fix tests? * Bugfix for references.pull/656/head
committed by
GitHub
178 changed files with 1345 additions and 1937 deletions
@ -1,36 +0,0 @@ |
|||
// ==========================================================================
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex UG (haftungsbeschränkt)
|
|||
// All rights reserved. Licensed under the MIT license.
|
|||
// ==========================================================================
|
|||
|
|||
using System; |
|||
using System.Collections.Generic; |
|||
using Newtonsoft.Json; |
|||
using Squidex.Infrastructure.Json.Newtonsoft; |
|||
|
|||
namespace Squidex.Domain.Apps.Core.Apps.Json |
|||
{ |
|||
public sealed class AppClientsConverter : JsonClassConverter<AppClients> |
|||
{ |
|||
protected override void WriteValue(JsonWriter writer, AppClients value, JsonSerializer serializer) |
|||
{ |
|||
var json = new Dictionary<string, AppClient>(value.Count); |
|||
|
|||
foreach (var (key, client) in value) |
|||
{ |
|||
json.Add(key, client); |
|||
} |
|||
|
|||
serializer.Serialize(writer, json); |
|||
} |
|||
|
|||
protected override AppClients ReadValue(JsonReader reader, Type objectType, JsonSerializer serializer) |
|||
{ |
|||
var json = serializer.Deserialize<Dictionary<string, AppClient>>(reader)!; |
|||
|
|||
return new AppClients(json); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,33 @@ |
|||
// ==========================================================================
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex UG (haftungsbeschränkt)
|
|||
// All rights reserved. Licensed under the MIT license.
|
|||
// ==========================================================================
|
|||
|
|||
using System.Collections.Generic; |
|||
using Squidex.Infrastructure; |
|||
|
|||
namespace Squidex.Domain.Apps.Core.Apps.Json |
|||
{ |
|||
public sealed class AppClientsSurrogate : Dictionary<string, AppClient>, ISurrogate<AppClients> |
|||
{ |
|||
public void FromSource(AppClients source) |
|||
{ |
|||
foreach (var (key, client) in source) |
|||
{ |
|||
Add(key, client); |
|||
} |
|||
} |
|||
|
|||
public AppClients ToSource() |
|||
{ |
|||
if (Count == 0) |
|||
{ |
|||
return AppClients.Empty; |
|||
} |
|||
|
|||
return new AppClients(this); |
|||
} |
|||
} |
|||
} |
|||
@ -1,36 +0,0 @@ |
|||
// ==========================================================================
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex UG (haftungsbeschränkt)
|
|||
// All rights reserved. Licensed under the MIT license.
|
|||
// ==========================================================================
|
|||
|
|||
using System; |
|||
using System.Collections.Generic; |
|||
using Newtonsoft.Json; |
|||
using Squidex.Infrastructure.Json.Newtonsoft; |
|||
|
|||
namespace Squidex.Domain.Apps.Core.Apps.Json |
|||
{ |
|||
public sealed class AppContributorsConverter : JsonClassConverter<AppContributors> |
|||
{ |
|||
protected override void WriteValue(JsonWriter writer, AppContributors value, JsonSerializer serializer) |
|||
{ |
|||
var json = new Dictionary<string, string>(value.Count); |
|||
|
|||
foreach (var (userId, role) in value) |
|||
{ |
|||
json.Add(userId, role); |
|||
} |
|||
|
|||
serializer.Serialize(writer, json); |
|||
} |
|||
|
|||
protected override AppContributors ReadValue(JsonReader reader, Type objectType, JsonSerializer serializer) |
|||
{ |
|||
var json = serializer.Deserialize<Dictionary<string, string>>(reader)!; |
|||
|
|||
return new AppContributors(json!); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,33 @@ |
|||
// ==========================================================================
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex UG (haftungsbeschränkt)
|
|||
// All rights reserved. Licensed under the MIT license.
|
|||
// ==========================================================================
|
|||
|
|||
using System.Collections.Generic; |
|||
using Squidex.Infrastructure; |
|||
|
|||
namespace Squidex.Domain.Apps.Core.Apps.Json |
|||
{ |
|||
public sealed class AppContributorsSurrogate : Dictionary<string, string>, ISurrogate<AppContributors> |
|||
{ |
|||
public void FromSource(AppContributors source) |
|||
{ |
|||
foreach (var (userId, role) in source) |
|||
{ |
|||
Add(userId, role); |
|||
} |
|||
} |
|||
|
|||
public AppContributors ToSource() |
|||
{ |
|||
if (Count == 0) |
|||
{ |
|||
return AppContributors.Empty; |
|||
} |
|||
|
|||
return new AppContributors(this); |
|||
} |
|||
} |
|||
} |
|||
@ -1,37 +0,0 @@ |
|||
// ==========================================================================
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex UG (haftungsbeschränkt)
|
|||
// All rights reserved. Licensed under the MIT license.
|
|||
// ==========================================================================
|
|||
|
|||
using System; |
|||
using System.Collections.Generic; |
|||
using Newtonsoft.Json; |
|||
using Squidex.Infrastructure; |
|||
using Squidex.Infrastructure.Json.Newtonsoft; |
|||
|
|||
namespace Squidex.Domain.Apps.Core.Apps.Json |
|||
{ |
|||
public sealed class AppPatternsConverter : JsonClassConverter<AppPatterns> |
|||
{ |
|||
protected override void WriteValue(JsonWriter writer, AppPatterns value, JsonSerializer serializer) |
|||
{ |
|||
var json = new Dictionary<DomainId, AppPattern>(value.Count); |
|||
|
|||
foreach (var (key, pattern) in value) |
|||
{ |
|||
json.Add(key, pattern); |
|||
} |
|||
|
|||
serializer.Serialize(writer, json); |
|||
} |
|||
|
|||
protected override AppPatterns ReadValue(JsonReader reader, Type objectType, JsonSerializer serializer) |
|||
{ |
|||
var json = serializer.Deserialize<Dictionary<DomainId, AppPattern>>(reader)!; |
|||
|
|||
return new AppPatterns(json); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,33 @@ |
|||
// ==========================================================================
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex UG (haftungsbeschränkt)
|
|||
// All rights reserved. Licensed under the MIT license.
|
|||
// ==========================================================================
|
|||
|
|||
using System.Collections.Generic; |
|||
using Squidex.Infrastructure; |
|||
|
|||
namespace Squidex.Domain.Apps.Core.Apps.Json |
|||
{ |
|||
public sealed class AppPatternsSurrogate : Dictionary<DomainId, AppPattern>, ISurrogate<AppPatterns> |
|||
{ |
|||
public void FromSource(AppPatterns source) |
|||
{ |
|||
foreach (var (key, pattern) in source) |
|||
{ |
|||
Add(key, pattern); |
|||
} |
|||
} |
|||
|
|||
public AppPatterns ToSource() |
|||
{ |
|||
if (Count == 0) |
|||
{ |
|||
return AppPatterns.Empty; |
|||
} |
|||
|
|||
return new AppPatterns(this); |
|||
} |
|||
} |
|||
} |
|||
@ -1,21 +0,0 @@ |
|||
// ==========================================================================
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex UG (haftungsbeschraenkt)
|
|||
// All rights reserved. Licensed under the MIT license.
|
|||
// ==========================================================================
|
|||
|
|||
using Newtonsoft.Json; |
|||
using Squidex.Infrastructure.Json.Objects; |
|||
|
|||
namespace Squidex.Domain.Apps.Core.Apps.Json |
|||
{ |
|||
public sealed class JsonRole |
|||
{ |
|||
[JsonProperty] |
|||
public string[] Permissions { get; set; } |
|||
|
|||
[JsonProperty] |
|||
public JsonObject Properties { get; set; } |
|||
} |
|||
} |
|||
@ -1,30 +0,0 @@ |
|||
// ==========================================================================
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex UG (haftungsbeschränkt)
|
|||
// All rights reserved. Licensed under the MIT license.
|
|||
// ==========================================================================
|
|||
|
|||
using System; |
|||
using Newtonsoft.Json; |
|||
using Squidex.Infrastructure.Json.Newtonsoft; |
|||
|
|||
namespace Squidex.Domain.Apps.Core.Apps.Json |
|||
{ |
|||
public sealed class LanguagesConfigConverter : JsonClassConverter<LanguagesConfig> |
|||
{ |
|||
protected override void WriteValue(JsonWriter writer, LanguagesConfig value, JsonSerializer serializer) |
|||
{ |
|||
var json = new JsonLanguagesConfig(value); |
|||
|
|||
serializer.Serialize(writer, json); |
|||
} |
|||
|
|||
protected override LanguagesConfig ReadValue(JsonReader reader, Type objectType, JsonSerializer serializer) |
|||
{ |
|||
var json = serializer.Deserialize<JsonLanguagesConfig>(reader)!; |
|||
|
|||
return json.ToConfig(); |
|||
} |
|||
} |
|||
} |
|||
@ -1,72 +0,0 @@ |
|||
// ==========================================================================
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex UG (haftungsbeschraenkt)
|
|||
// All rights reserved. Licensed under the MIT license.
|
|||
// ==========================================================================
|
|||
|
|||
using System; |
|||
using Newtonsoft.Json; |
|||
using Squidex.Infrastructure.Json.Newtonsoft; |
|||
using Squidex.Infrastructure.Json.Objects; |
|||
|
|||
namespace Squidex.Domain.Apps.Core.Apps.Json |
|||
{ |
|||
public sealed class RoleConverter : JsonClassConverter<JsonRole> |
|||
{ |
|||
protected override void WriteValue(JsonWriter writer, JsonRole value, JsonSerializer serializer) |
|||
{ |
|||
writer.WriteStartObject(); |
|||
|
|||
writer.WritePropertyName("permissions"); |
|||
serializer.Serialize(writer, value.Permissions); |
|||
|
|||
writer.WritePropertyName("properties"); |
|||
serializer.Serialize(writer, value.Properties); |
|||
|
|||
writer.WriteEndObject(); |
|||
} |
|||
|
|||
protected override JsonRole ReadValue(JsonReader reader, Type objectType, JsonSerializer serializer) |
|||
{ |
|||
var permissions = Array.Empty<string>(); |
|||
var properties = (JsonObject?)null; |
|||
|
|||
if (reader.TokenType == JsonToken.StartArray) |
|||
{ |
|||
permissions = serializer.Deserialize<string[]>(reader)!; |
|||
} |
|||
else |
|||
{ |
|||
while (reader.Read() && reader.TokenType != JsonToken.EndObject) |
|||
{ |
|||
if (reader.TokenType == JsonToken.PropertyName) |
|||
{ |
|||
var propertyName = reader.Value!.ToString()!; |
|||
|
|||
if (!reader.Read()) |
|||
{ |
|||
throw new JsonSerializationException("Unexpected end when reading role."); |
|||
} |
|||
|
|||
switch (propertyName.ToLowerInvariant()) |
|||
{ |
|||
case "permissions": |
|||
permissions = serializer.Deserialize<string[]>(reader)!; |
|||
break; |
|||
case "properties": |
|||
properties = serializer.Deserialize<JsonObject>(reader)!; |
|||
break; |
|||
} |
|||
} |
|||
} |
|||
} |
|||
|
|||
return new JsonRole |
|||
{ |
|||
Permissions = permissions, |
|||
Properties = properties ?? JsonValue.Object() |
|||
}; |
|||
} |
|||
} |
|||
} |
|||
@ -1,59 +0,0 @@ |
|||
// ==========================================================================
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex UG (haftungsbeschränkt)
|
|||
// All rights reserved. Licensed under the MIT license.
|
|||
// ==========================================================================
|
|||
|
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using Newtonsoft.Json; |
|||
using Squidex.Infrastructure.Json.Newtonsoft; |
|||
using Squidex.Infrastructure.Security; |
|||
|
|||
namespace Squidex.Domain.Apps.Core.Apps.Json |
|||
{ |
|||
public sealed class RolesConverter : JsonClassConverter<Roles> |
|||
{ |
|||
protected override void WriteValue(JsonWriter writer, Roles value, JsonSerializer serializer) |
|||
{ |
|||
var json = new Dictionary<string, JsonRole>(value.CustomCount); |
|||
|
|||
foreach (var role in value.Custom) |
|||
{ |
|||
json.Add(role.Name, new JsonRole |
|||
{ |
|||
Permissions = role.Permissions.ToIds().ToArray(), |
|||
Properties = role.Properties |
|||
}); |
|||
} |
|||
|
|||
serializer.Serialize(writer, json); |
|||
} |
|||
|
|||
protected override Roles ReadValue(JsonReader reader, Type objectType, JsonSerializer serializer) |
|||
{ |
|||
var json = serializer.Deserialize<Dictionary<string, JsonRole>>(reader)!; |
|||
|
|||
if (json.Count == 0) |
|||
{ |
|||
return Roles.Empty; |
|||
} |
|||
|
|||
return new Roles(json.ToDictionary(x => x.Key, x => |
|||
{ |
|||
var (key, value) = x; |
|||
|
|||
var permissions = PermissionSet.Empty; |
|||
|
|||
if (value.Permissions.Length > 0) |
|||
{ |
|||
permissions = new PermissionSet(value.Permissions); |
|||
} |
|||
|
|||
return new Role(key, permissions, value.Properties); |
|||
})); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,76 @@ |
|||
// ==========================================================================
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex UG (haftungsbeschränkt)
|
|||
// All rights reserved. Licensed under the MIT license.
|
|||
// ==========================================================================
|
|||
|
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using Squidex.Infrastructure; |
|||
using Squidex.Infrastructure.Json.Objects; |
|||
using Squidex.Infrastructure.Security; |
|||
|
|||
namespace Squidex.Domain.Apps.Core.Apps.Json |
|||
{ |
|||
public sealed class RolesSurrogate : Dictionary<string, IJsonValue>, ISurrogate<Roles> |
|||
{ |
|||
public void FromSource(Roles source) |
|||
{ |
|||
foreach (var customRole in source.Custom) |
|||
{ |
|||
var permissions = JsonValue.Array(); |
|||
|
|||
foreach (var permission in customRole.Permissions) |
|||
{ |
|||
permissions.Add(JsonValue.Create(permission.Id)); |
|||
} |
|||
|
|||
var role = |
|||
JsonValue.Object() |
|||
.Add("permissions", permissions) |
|||
.Add("properties", customRole.Properties); |
|||
|
|||
Add(customRole.Name, role); |
|||
} |
|||
} |
|||
|
|||
public Roles ToSource() |
|||
{ |
|||
if (Count == 0) |
|||
{ |
|||
return Roles.Empty; |
|||
} |
|||
|
|||
return new Roles(this.ToDictionary(x => x.Key, x => |
|||
{ |
|||
var (key, value) = x; |
|||
|
|||
var properties = JsonValue.Object(); |
|||
var permissions = PermissionSet.Empty; |
|||
|
|||
if (value is JsonArray array) |
|||
{ |
|||
if (array.Count > 0) |
|||
{ |
|||
permissions = new PermissionSet(array.OfType<JsonString>().Select(x => x.ToString())); |
|||
} |
|||
} |
|||
else if (value is JsonObject obj) |
|||
{ |
|||
if (obj.TryGetValue("permissions", out array!) && array.Count > 0) |
|||
{ |
|||
permissions = new PermissionSet(array.OfType<JsonString>().Select(x => x.ToString())); |
|||
} |
|||
|
|||
if (!obj.TryGetValue<JsonObject>("properties", out properties)) |
|||
{ |
|||
properties = JsonValue.Object(); |
|||
} |
|||
} |
|||
|
|||
return new Role(key, permissions, properties); |
|||
})); |
|||
} |
|||
} |
|||
} |
|||
@ -1,39 +0,0 @@ |
|||
// ==========================================================================
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex UG (haftungsbeschränkt)
|
|||
// All rights reserved. Licensed under the MIT license.
|
|||
// ==========================================================================
|
|||
|
|||
using System; |
|||
using System.Collections.Generic; |
|||
using Newtonsoft.Json; |
|||
using Squidex.Infrastructure.Json.Newtonsoft; |
|||
|
|||
namespace Squidex.Domain.Apps.Core.Contents.Json |
|||
{ |
|||
public sealed class StatusConverter : JsonConverter, ISupportedTypes |
|||
{ |
|||
public IEnumerable<Type> SupportedTypes |
|||
{ |
|||
get { yield return typeof(Status); } |
|||
} |
|||
|
|||
public override void WriteJson(JsonWriter writer, object? value, JsonSerializer serializer) |
|||
{ |
|||
writer.WriteValue(value!.ToString()); |
|||
} |
|||
|
|||
public override object ReadJson(JsonReader reader, Type objectType, object? existingValue, JsonSerializer serializer) |
|||
{ |
|||
var value = serializer.Deserialize<string>(reader)!; |
|||
|
|||
return new Status(value); |
|||
} |
|||
|
|||
public override bool CanConvert(Type objectType) |
|||
{ |
|||
return objectType == typeof(Status); |
|||
} |
|||
} |
|||
} |
|||
@ -1,30 +0,0 @@ |
|||
// ==========================================================================
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex UG (haftungsbeschraenkt)
|
|||
// All rights reserved. Licensed under the MIT license.
|
|||
// ==========================================================================
|
|||
|
|||
using System; |
|||
using Newtonsoft.Json; |
|||
using Squidex.Infrastructure.Json.Newtonsoft; |
|||
|
|||
namespace Squidex.Domain.Apps.Core.Contents.Json |
|||
{ |
|||
public sealed class WorkflowStepConverter : JsonClassConverter<WorkflowStep> |
|||
{ |
|||
protected override void WriteValue(JsonWriter writer, WorkflowStep value, JsonSerializer serializer) |
|||
{ |
|||
var json = new JsonWorkflowStep(value); |
|||
|
|||
serializer.Serialize(writer, json); |
|||
} |
|||
|
|||
protected override WorkflowStep ReadValue(JsonReader reader, Type objectType, JsonSerializer serializer) |
|||
{ |
|||
var json = serializer.Deserialize<JsonWorkflowStep>(reader)!; |
|||
|
|||
return json.ToStep(); |
|||
} |
|||
} |
|||
} |
|||
@ -1,37 +0,0 @@ |
|||
// ==========================================================================
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex UG (haftungsbeschraenkt)
|
|||
// All rights reserved. Licensed under the MIT license.
|
|||
// ==========================================================================
|
|||
|
|||
using System; |
|||
using System.Collections.Generic; |
|||
using Newtonsoft.Json; |
|||
using Squidex.Infrastructure; |
|||
using Squidex.Infrastructure.Json.Newtonsoft; |
|||
|
|||
namespace Squidex.Domain.Apps.Core.Contents.Json |
|||
{ |
|||
public sealed class WorkflowsConverter : JsonClassConverter<Workflows> |
|||
{ |
|||
protected override void WriteValue(JsonWriter writer, Workflows value, JsonSerializer serializer) |
|||
{ |
|||
var json = new Dictionary<DomainId, Workflow>(value.Count); |
|||
|
|||
foreach (var (key, workflow) in value) |
|||
{ |
|||
json.Add(key, workflow); |
|||
} |
|||
|
|||
serializer.Serialize(writer, json); |
|||
} |
|||
|
|||
protected override Workflows ReadValue(JsonReader reader, Type objectType, JsonSerializer serializer) |
|||
{ |
|||
var json = serializer.Deserialize<Dictionary<DomainId, Workflow>>(reader); |
|||
|
|||
return new Workflows(json!); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,28 @@ |
|||
// ==========================================================================
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex UG (haftungsbeschraenkt)
|
|||
// All rights reserved. Licensed under the MIT license.
|
|||
// ==========================================================================
|
|||
|
|||
using System.Collections.Generic; |
|||
using Squidex.Infrastructure; |
|||
|
|||
namespace Squidex.Domain.Apps.Core.Contents.Json |
|||
{ |
|||
public sealed class WorkflowsSurrogate : Dictionary<DomainId, Workflow>, ISurrogate<Workflows> |
|||
{ |
|||
public void FromSource(Workflows source) |
|||
{ |
|||
foreach (var (key, workflow) in source) |
|||
{ |
|||
Add(key, workflow); |
|||
} |
|||
} |
|||
|
|||
public Workflows ToSource() |
|||
{ |
|||
return new Workflows(this); |
|||
} |
|||
} |
|||
} |
|||
@ -1,26 +0,0 @@ |
|||
// ==========================================================================
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex UG (haftungsbeschränkt)
|
|||
// All rights reserved. Licensed under the MIT license.
|
|||
// ==========================================================================
|
|||
|
|||
using System; |
|||
using Newtonsoft.Json; |
|||
using Squidex.Infrastructure.Json.Newtonsoft; |
|||
|
|||
namespace Squidex.Domain.Apps.Core.Rules.Json |
|||
{ |
|||
public sealed class RuleConverter : JsonClassConverter<Rule> |
|||
{ |
|||
protected override void WriteValue(JsonWriter writer, Rule value, JsonSerializer serializer) |
|||
{ |
|||
serializer.Serialize(writer, new JsonRule(value)); |
|||
} |
|||
|
|||
protected override Rule ReadValue(JsonReader reader, Type objectType, JsonSerializer serializer) |
|||
{ |
|||
return serializer.Deserialize<JsonRule>(reader)!.ToRule(); |
|||
} |
|||
} |
|||
} |
|||
@ -1,37 +0,0 @@ |
|||
// ==========================================================================
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex UG (haftungsbeschraenkt)
|
|||
// All rights reserved. Licensed under the MIT license.
|
|||
// ==========================================================================
|
|||
|
|||
using Newtonsoft.Json; |
|||
|
|||
namespace Squidex.Domain.Apps.Core.Schemas.Json |
|||
{ |
|||
public sealed class JsonNestedFieldModel : IFieldSettings |
|||
{ |
|||
[JsonProperty] |
|||
public long Id { get; set; } |
|||
|
|||
[JsonProperty] |
|||
public string Name { get; set; } |
|||
|
|||
[JsonProperty] |
|||
public bool IsHidden { get; set; } |
|||
|
|||
[JsonProperty] |
|||
public bool IsLocked { get; set; } |
|||
|
|||
[JsonProperty] |
|||
public bool IsDisabled { get; set; } |
|||
|
|||
[JsonProperty] |
|||
public FieldProperties Properties { get; set; } |
|||
|
|||
public NestedField ToNestedField() |
|||
{ |
|||
return Properties.CreateNestedField(Id, Name, this); |
|||
} |
|||
} |
|||
} |
|||
@ -1,26 +0,0 @@ |
|||
// ==========================================================================
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex UG (haftungsbeschränkt)
|
|||
// All rights reserved. Licensed under the MIT license.
|
|||
// ==========================================================================
|
|||
|
|||
using System; |
|||
using Newtonsoft.Json; |
|||
using Squidex.Infrastructure.Json.Newtonsoft; |
|||
|
|||
namespace Squidex.Domain.Apps.Core.Schemas.Json |
|||
{ |
|||
public sealed class SchemaConverter : JsonClassConverter<Schema> |
|||
{ |
|||
protected override void WriteValue(JsonWriter writer, Schema value, JsonSerializer serializer) |
|||
{ |
|||
serializer.Serialize(writer, new JsonSchemaModel(value)); |
|||
} |
|||
|
|||
protected override Schema ReadValue(JsonReader reader, Type objectType, JsonSerializer serializer) |
|||
{ |
|||
return serializer.Deserialize<JsonSchemaModel>(reader)!.ToSchema(); |
|||
} |
|||
} |
|||
} |
|||
@ -1,53 +0,0 @@ |
|||
// ==========================================================================
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex UG (haftungsbeschraenkt)
|
|||
// All rights reserved. Licensed under the MIT license.
|
|||
// ==========================================================================
|
|||
|
|||
using MongoDB.Bson; |
|||
using MongoDB.Bson.Serialization; |
|||
using MongoDB.Bson.Serialization.Serializers; |
|||
using Newtonsoft.Json.Linq; |
|||
|
|||
namespace Squidex.Infrastructure.MongoDb |
|||
{ |
|||
public sealed class JTokenSerializer<T> : ClassSerializerBase<T?> where T : JToken |
|||
{ |
|||
public static readonly JTokenSerializer<T> Instance = new JTokenSerializer<T>(); |
|||
|
|||
public override T? Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args) |
|||
{ |
|||
var bsonReader = context.Reader; |
|||
|
|||
if (bsonReader.GetCurrentBsonType() == BsonType.Null) |
|||
{ |
|||
bsonReader.ReadNull(); |
|||
|
|||
return null; |
|||
} |
|||
else |
|||
{ |
|||
var jsonReader = new BsonJsonReader(bsonReader); |
|||
|
|||
return (T)JToken.ReadFrom(jsonReader); |
|||
} |
|||
} |
|||
|
|||
public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, T? value) |
|||
{ |
|||
var bsonWriter = context.Writer; |
|||
|
|||
if (value == null) |
|||
{ |
|||
bsonWriter.WriteNull(); |
|||
} |
|||
else |
|||
{ |
|||
var jsonWriter = new BsonJsonWriter(bsonWriter); |
|||
|
|||
value.WriteTo(jsonWriter); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
@ -1,40 +0,0 @@ |
|||
// ==========================================================================
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex UG (haftungsbeschränkt)
|
|||
// All rights reserved. Licensed under the MIT license.
|
|||
// ==========================================================================
|
|||
|
|||
using MongoDB.Bson; |
|||
using MongoDB.Bson.Serialization; |
|||
using MongoDB.Bson.Serialization.Serializers; |
|||
|
|||
namespace Squidex.Infrastructure.MongoDb |
|||
{ |
|||
public class RefTokenSerializer : ClassSerializerBase<RefToken> |
|||
{ |
|||
public static void Register() |
|||
{ |
|||
try |
|||
{ |
|||
BsonSerializer.RegisterSerializer(new RefTokenSerializer()); |
|||
} |
|||
catch (BsonSerializationException) |
|||
{ |
|||
return; |
|||
} |
|||
} |
|||
|
|||
protected override RefToken DeserializeValue(BsonDeserializationContext context, BsonDeserializationArgs args) |
|||
{ |
|||
var value = context.Reader.ReadString(); |
|||
|
|||
return RefToken.Parse(value); |
|||
} |
|||
|
|||
protected override void SerializeValue(BsonSerializationContext context, BsonSerializationArgs args, RefToken value) |
|||
{ |
|||
context.Writer.WriteString(value.ToString()); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,78 @@ |
|||
// ==========================================================================
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex UG (haftungsbeschraenkt)
|
|||
// All rights reserved. Licensed under the MIT license.
|
|||
// ==========================================================================
|
|||
|
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Security.Claims; |
|||
|
|||
namespace Squidex.Infrastructure.Json |
|||
{ |
|||
public sealed class ClaimsPrinicpalSurrogate : List<ClaimsIdentitySurrogate>, ISurrogate<ClaimsPrincipal> |
|||
{ |
|||
public void FromSource(ClaimsPrincipal source) |
|||
{ |
|||
foreach (var identity in source.Identities) |
|||
{ |
|||
var surrogate = new ClaimsIdentitySurrogate(); |
|||
|
|||
surrogate.FromSource(identity); |
|||
|
|||
Add(surrogate); |
|||
} |
|||
} |
|||
|
|||
public ClaimsPrincipal ToSource() |
|||
{ |
|||
return new ClaimsPrincipal(this.Select(x => x.ToSource())); |
|||
} |
|||
} |
|||
|
|||
public sealed class ClaimsIdentitySurrogate : ISurrogate<ClaimsIdentity> |
|||
{ |
|||
public string? AuthenticationType { get; set; } |
|||
|
|||
public ClaimSurrogate[] Claims { get; set; } |
|||
|
|||
public void FromSource(ClaimsIdentity source) |
|||
{ |
|||
AuthenticationType = source.AuthenticationType; |
|||
|
|||
Claims = source.Claims.Select(claim => |
|||
{ |
|||
var surrogate = new ClaimSurrogate(); |
|||
|
|||
surrogate.FromSource(claim); |
|||
|
|||
return surrogate; |
|||
}).ToArray(); |
|||
} |
|||
|
|||
public ClaimsIdentity ToSource() |
|||
{ |
|||
return new ClaimsIdentity(Claims.Select(x => x.ToSource()), AuthenticationType); |
|||
} |
|||
} |
|||
|
|||
public sealed class ClaimSurrogate : ISurrogate<Claim> |
|||
{ |
|||
public string Type { get; set; } |
|||
|
|||
public string Value { get; set; } |
|||
|
|||
public void FromSource(Claim source) |
|||
{ |
|||
Type = source.Type; |
|||
|
|||
Value = source.Value; |
|||
} |
|||
|
|||
public Claim ToSource() |
|||
{ |
|||
return new Claim(Type, Value); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,35 @@ |
|||
// ==========================================================================
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex UG (haftungsbeschraenkt)
|
|||
// All rights reserved. Licensed under the MIT license.
|
|||
// ==========================================================================
|
|||
|
|||
using System; |
|||
using System.Runtime.Serialization; |
|||
|
|||
namespace Squidex.Infrastructure.Json |
|||
{ |
|||
[Serializable] |
|||
public class JsonException : Exception |
|||
{ |
|||
public JsonException() |
|||
{ |
|||
} |
|||
|
|||
public JsonException(string message) |
|||
: base(message) |
|||
{ |
|||
} |
|||
|
|||
public JsonException(string message, Exception inner) |
|||
: base(message, inner) |
|||
{ |
|||
} |
|||
|
|||
protected JsonException(SerializationInfo info, StreamingContext context) |
|||
: base(info, context) |
|||
{ |
|||
} |
|||
} |
|||
} |
|||
@ -1,62 +0,0 @@ |
|||
// ==========================================================================
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex UG (haftungsbeschränkt)
|
|||
// All rights reserved. Licensed under the MIT license.
|
|||
// ==========================================================================
|
|||
|
|||
using System; |
|||
using System.Linq; |
|||
using System.Security.Claims; |
|||
using Newtonsoft.Json; |
|||
|
|||
namespace Squidex.Infrastructure.Json.Newtonsoft |
|||
{ |
|||
public sealed class ClaimsPrincipalConverter : JsonClassConverter<ClaimsPrincipal> |
|||
{ |
|||
private sealed class JsonIdentity |
|||
{ |
|||
[JsonProperty] |
|||
public string AuthenticationType { get; set; } |
|||
|
|||
[JsonProperty] |
|||
public JsonClaim[] Claims { get; set; } |
|||
} |
|||
|
|||
private sealed class JsonClaim |
|||
{ |
|||
[JsonProperty] |
|||
public string Type { get; set; } |
|||
|
|||
[JsonProperty] |
|||
public string Value { get; set; } |
|||
} |
|||
|
|||
protected override void WriteValue(JsonWriter writer, ClaimsPrincipal value, JsonSerializer serializer) |
|||
{ |
|||
var jsonIdentities = |
|||
value.Identities.Select(identity => |
|||
new JsonIdentity |
|||
{ |
|||
Claims = identity.Claims.Select(c => |
|||
{ |
|||
return new JsonClaim { Type = c.Type, Value = c.Value }; |
|||
}).ToArray(), |
|||
AuthenticationType = identity.AuthenticationType! |
|||
}).ToArray(); |
|||
|
|||
serializer.Serialize(writer, jsonIdentities); |
|||
} |
|||
|
|||
protected override ClaimsPrincipal ReadValue(JsonReader reader, Type objectType, JsonSerializer serializer) |
|||
{ |
|||
var jsonIdentities = serializer.Deserialize<JsonIdentity[]>(reader)!; |
|||
|
|||
return new ClaimsPrincipal( |
|||
jsonIdentities.Select(identity => |
|||
new ClaimsIdentity( |
|||
identity.Claims.Select(c => new Claim(c.Type, c.Value)), |
|||
identity.AuthenticationType))); |
|||
} |
|||
} |
|||
} |
|||
@ -1,62 +0,0 @@ |
|||
// ==========================================================================
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex UG (haftungsbeschraenkt)
|
|||
// All rights reserved. Licensed under the MIT license.
|
|||
// ==========================================================================
|
|||
|
|||
using System; |
|||
using System.Collections.Generic; |
|||
using Newtonsoft.Json; |
|||
|
|||
namespace Squidex.Infrastructure.Json.Newtonsoft |
|||
{ |
|||
public sealed class DomainIdConverter : JsonConverter, ISupportedTypes |
|||
{ |
|||
public IEnumerable<Type> SupportedTypes |
|||
{ |
|||
get |
|||
{ |
|||
yield return typeof(DomainId); |
|||
yield return typeof(DomainId?); |
|||
} |
|||
} |
|||
|
|||
public override void WriteJson(JsonWriter writer, object? value, JsonSerializer serializer) |
|||
{ |
|||
if (value != null) |
|||
{ |
|||
writer.WriteValue(value.ToString()); |
|||
} |
|||
else |
|||
{ |
|||
writer.WriteNull(); |
|||
} |
|||
} |
|||
|
|||
public override object? ReadJson(JsonReader reader, Type objectType, object? existingValue, JsonSerializer serializer) |
|||
{ |
|||
if (reader.Value == null) |
|||
{ |
|||
return null; |
|||
} |
|||
|
|||
if (reader.TokenType == JsonToken.String) |
|||
{ |
|||
return DomainId.Create(reader.Value.ToString()!); |
|||
} |
|||
|
|||
if (reader.TokenType == JsonToken.Null && objectType == typeof(DomainId?)) |
|||
{ |
|||
return null; |
|||
} |
|||
|
|||
throw new JsonException($"Not a valid date time, expected String, but got {reader.TokenType}."); |
|||
} |
|||
|
|||
public override bool CanConvert(Type objectType) |
|||
{ |
|||
return objectType == typeof(DomainId) || objectType == typeof(DomainId?); |
|||
} |
|||
} |
|||
} |
|||
@ -1,69 +0,0 @@ |
|||
// ==========================================================================
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex UG (haftungsbeschränkt)
|
|||
// All rights reserved. Licensed under the MIT license.
|
|||
// ==========================================================================
|
|||
|
|||
using System; |
|||
using System.Collections.Generic; |
|||
using Newtonsoft.Json; |
|||
using NodaTime; |
|||
using NodaTime.Text; |
|||
|
|||
namespace Squidex.Infrastructure.Json.Newtonsoft |
|||
{ |
|||
public sealed class InstantConverter : JsonConverter, ISupportedTypes |
|||
{ |
|||
public IEnumerable<Type> SupportedTypes |
|||
{ |
|||
get |
|||
{ |
|||
yield return typeof(Instant); |
|||
yield return typeof(Instant?); |
|||
} |
|||
} |
|||
|
|||
public override void WriteJson(JsonWriter writer, object? value, JsonSerializer serializer) |
|||
{ |
|||
if (value != null) |
|||
{ |
|||
writer.WriteValue(value.ToString()); |
|||
} |
|||
else |
|||
{ |
|||
writer.WriteNull(); |
|||
} |
|||
} |
|||
|
|||
public override object? ReadJson(JsonReader reader, Type objectType, object? existingValue, JsonSerializer serializer) |
|||
{ |
|||
if (reader.Value == null) |
|||
{ |
|||
return null; |
|||
} |
|||
|
|||
if (reader.TokenType == JsonToken.String) |
|||
{ |
|||
return InstantPattern.General.Parse(reader.Value.ToString()!).Value; |
|||
} |
|||
|
|||
if (reader.TokenType == JsonToken.Date) |
|||
{ |
|||
return Instant.FromDateTimeUtc((DateTime)reader.Value); |
|||
} |
|||
|
|||
if (reader.TokenType == JsonToken.Null && objectType == typeof(Instant?)) |
|||
{ |
|||
return null; |
|||
} |
|||
|
|||
throw new JsonException($"Not a valid date time, expected String or Date, but got {reader.TokenType}."); |
|||
} |
|||
|
|||
public override bool CanConvert(Type objectType) |
|||
{ |
|||
return objectType == typeof(Instant) || objectType == typeof(Instant?); |
|||
} |
|||
} |
|||
} |
|||
@ -1,27 +0,0 @@ |
|||
// ==========================================================================
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex UG (haftungsbeschränkt)
|
|||
// All rights reserved. Licensed under the MIT license.
|
|||
// ==========================================================================
|
|||
|
|||
using System; |
|||
using Newtonsoft.Json; |
|||
|
|||
namespace Squidex.Infrastructure.Json.Newtonsoft |
|||
{ |
|||
public sealed class LanguageConverter : JsonClassConverter<Language> |
|||
{ |
|||
protected override void WriteValue(JsonWriter writer, Language value, JsonSerializer serializer) |
|||
{ |
|||
writer.WriteValue(value.Iso2Code); |
|||
} |
|||
|
|||
protected override Language ReadValue(JsonReader reader, Type objectType, JsonSerializer serializer) |
|||
{ |
|||
var value = serializer.Deserialize<string>(reader)!; |
|||
|
|||
return Language.GetLanguage(value); |
|||
} |
|||
} |
|||
} |
|||
@ -1,41 +0,0 @@ |
|||
// ==========================================================================
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex UG (haftungsbeschränkt)
|
|||
// All rights reserved. Licensed under the MIT license.
|
|||
// ==========================================================================
|
|||
|
|||
using System; |
|||
using Newtonsoft.Json; |
|||
|
|||
namespace Squidex.Infrastructure.Json.Newtonsoft |
|||
{ |
|||
public sealed class NamedDomainIdConverter : JsonClassConverter<NamedId<DomainId>> |
|||
{ |
|||
private static readonly Parser<DomainId> Parser = ParseString; |
|||
|
|||
protected override void WriteValue(JsonWriter writer, NamedId<DomainId> value, JsonSerializer serializer) |
|||
{ |
|||
writer.WriteValue(value.ToString()); |
|||
} |
|||
|
|||
protected override NamedId<DomainId> ReadValue(JsonReader reader, Type objectType, JsonSerializer serializer) |
|||
{ |
|||
var value = serializer.Deserialize<string>(reader)!; |
|||
|
|||
if (!NamedId<DomainId>.TryParse(value, Parser, out var result)) |
|||
{ |
|||
throw new JsonException("Named id must have at least 2 parts divided by comma."); |
|||
} |
|||
|
|||
return result; |
|||
} |
|||
|
|||
private static bool ParseString(ReadOnlySpan<char> value, out DomainId result) |
|||
{ |
|||
result = DomainId.Create(new string(value)); |
|||
|
|||
return true; |
|||
} |
|||
} |
|||
} |
|||
@ -1,34 +0,0 @@ |
|||
// ==========================================================================
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex UG (haftungsbeschränkt)
|
|||
// All rights reserved. Licensed under the MIT license.
|
|||
// ==========================================================================
|
|||
|
|||
using System; |
|||
using Newtonsoft.Json; |
|||
|
|||
namespace Squidex.Infrastructure.Json.Newtonsoft |
|||
{ |
|||
public sealed class NamedGuidIdConverter : JsonClassConverter<NamedId<Guid>> |
|||
{ |
|||
private static readonly Parser<Guid> Parser = Guid.TryParse; |
|||
|
|||
protected override void WriteValue(JsonWriter writer, NamedId<Guid> value, JsonSerializer serializer) |
|||
{ |
|||
writer.WriteValue(value.ToString()); |
|||
} |
|||
|
|||
protected override NamedId<Guid> ReadValue(JsonReader reader, Type objectType, JsonSerializer serializer) |
|||
{ |
|||
var value = serializer.Deserialize<string>(reader)!; |
|||
|
|||
if (!NamedId<Guid>.TryParse(value, Parser, out var result)) |
|||
{ |
|||
throw new JsonException("Named id must have more than 2 parts divided by commata."); |
|||
} |
|||
|
|||
return result; |
|||
} |
|||
} |
|||
} |
|||
@ -1,34 +0,0 @@ |
|||
// ==========================================================================
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex UG (haftungsbeschränkt)
|
|||
// All rights reserved. Licensed under the MIT license.
|
|||
// ==========================================================================
|
|||
|
|||
using System; |
|||
using Newtonsoft.Json; |
|||
|
|||
namespace Squidex.Infrastructure.Json.Newtonsoft |
|||
{ |
|||
public sealed class NamedLongIdConverter : JsonClassConverter<NamedId<long>> |
|||
{ |
|||
private static readonly Parser<long> Parser = long.TryParse; |
|||
|
|||
protected override void WriteValue(JsonWriter writer, NamedId<long> value, JsonSerializer serializer) |
|||
{ |
|||
writer.WriteValue(value.ToString()); |
|||
} |
|||
|
|||
protected override NamedId<long> ReadValue(JsonReader reader, Type objectType, JsonSerializer serializer) |
|||
{ |
|||
var value = serializer.Deserialize<string>(reader)!; |
|||
|
|||
if (!NamedId<long>.TryParse(value, Parser, out var result)) |
|||
{ |
|||
throw new JsonException("Named id must have at least 2 parts divided by commata."); |
|||
} |
|||
|
|||
return result; |
|||
} |
|||
} |
|||
} |
|||
@ -1,41 +0,0 @@ |
|||
// ==========================================================================
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex UG (haftungsbeschränkt)
|
|||
// All rights reserved. Licensed under the MIT license.
|
|||
// ==========================================================================
|
|||
|
|||
using System; |
|||
using Newtonsoft.Json; |
|||
|
|||
namespace Squidex.Infrastructure.Json.Newtonsoft |
|||
{ |
|||
public sealed class NamedStringIdConverter : JsonClassConverter<NamedId<string>> |
|||
{ |
|||
private static readonly Parser<string> Parser = ParseString; |
|||
|
|||
protected override void WriteValue(JsonWriter writer, NamedId<string> value, JsonSerializer serializer) |
|||
{ |
|||
writer.WriteValue(value.ToString()); |
|||
} |
|||
|
|||
protected override NamedId<string> ReadValue(JsonReader reader, Type objectType, JsonSerializer serializer) |
|||
{ |
|||
var value = serializer.Deserialize<string>(reader)!; |
|||
|
|||
if (!NamedId<string>.TryParse(value, Parser, out var result)) |
|||
{ |
|||
throw new JsonException("Named id must have at least 2 parts divided by commata."); |
|||
} |
|||
|
|||
return result; |
|||
} |
|||
|
|||
private static bool ParseString(ReadOnlySpan<char> value, out string result) |
|||
{ |
|||
result = new string(value); |
|||
|
|||
return true; |
|||
} |
|||
} |
|||
} |
|||
@ -1,32 +0,0 @@ |
|||
// ==========================================================================
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex UG (haftungsbeschränkt)
|
|||
// All rights reserved. Licensed under the MIT license.
|
|||
// ==========================================================================
|
|||
|
|||
using System; |
|||
using Newtonsoft.Json; |
|||
|
|||
namespace Squidex.Infrastructure.Json.Newtonsoft |
|||
{ |
|||
public sealed class RefTokenConverter : JsonClassConverter<RefToken> |
|||
{ |
|||
protected override void WriteValue(JsonWriter writer, RefToken value, JsonSerializer serializer) |
|||
{ |
|||
writer.WriteValue(value.ToString()); |
|||
} |
|||
|
|||
protected override RefToken ReadValue(JsonReader reader, Type objectType, JsonSerializer serializer) |
|||
{ |
|||
var value = serializer.Deserialize<string>(reader)!; |
|||
|
|||
if (!RefToken.TryParse(value, out var result)) |
|||
{ |
|||
throw new JsonException("Named id must have at least 2 parts divided by colon."); |
|||
} |
|||
|
|||
return result; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,31 @@ |
|||
// ==========================================================================
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex UG (haftungsbeschraenkt)
|
|||
// All rights reserved. Licensed under the MIT license.
|
|||
// ==========================================================================
|
|||
|
|||
using System; |
|||
using Newtonsoft.Json; |
|||
|
|||
namespace Squidex.Infrastructure.Json.Newtonsoft |
|||
{ |
|||
public sealed class SurrogateConverter<T, TSurrogate> : JsonClassConverter<T> where T : class where TSurrogate : ISurrogate<T>, new() |
|||
{ |
|||
protected override T ReadValue(JsonReader reader, Type objectType, JsonSerializer serializer) |
|||
{ |
|||
var surrogate = serializer.Deserialize<TSurrogate>(reader); |
|||
|
|||
return surrogate!.ToSource(); |
|||
} |
|||
|
|||
protected override void WriteValue(JsonWriter writer, T value, JsonSerializer serializer) |
|||
{ |
|||
var surrogate = new TSurrogate(); |
|||
|
|||
surrogate.FromSource(value); |
|||
|
|||
serializer.Serialize(writer, surrogate); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,36 @@ |
|||
// ==========================================================================
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex UG (haftungsbeschraenkt)
|
|||
// All rights reserved. Licensed under the MIT license.
|
|||
// ==========================================================================
|
|||
|
|||
using System; |
|||
using System.ComponentModel; |
|||
using System.Globalization; |
|||
|
|||
namespace Squidex.Infrastructure |
|||
{ |
|||
public sealed class LanguageTypeConverter : TypeConverter |
|||
{ |
|||
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) |
|||
{ |
|||
return sourceType == typeof(string); |
|||
} |
|||
|
|||
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) |
|||
{ |
|||
return destinationType == typeof(string); |
|||
} |
|||
|
|||
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) |
|||
{ |
|||
return Language.GetLanguage((string)value); |
|||
} |
|||
|
|||
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) |
|||
{ |
|||
return ((Language)value).Iso2Code; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,83 @@ |
|||
// ==========================================================================
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex UG (haftungsbeschraenkt)
|
|||
// All rights reserved. Licensed under the MIT license.
|
|||
// ==========================================================================
|
|||
|
|||
using System; |
|||
using System.ComponentModel; |
|||
using System.Globalization; |
|||
|
|||
namespace Squidex.Infrastructure |
|||
{ |
|||
internal sealed class NamedIdTypeConverter : TypeConverter |
|||
{ |
|||
private static readonly Parser<Guid> ParserGuid = Guid.TryParse; |
|||
private static readonly Parser<DomainId> ParserDomainId = ParseDomainId; |
|||
private static readonly Parser<string> ParserString = ParseString; |
|||
private static readonly Parser<long> ParserLong = long.TryParse; |
|||
private readonly Func<string, object>? converter; |
|||
|
|||
public NamedIdTypeConverter(Type type) |
|||
{ |
|||
var genericType = type?.GetGenericArguments()?[0]; |
|||
|
|||
if (genericType == typeof(Guid)) |
|||
{ |
|||
converter = v => NamedId<Guid>.Parse(v, ParserGuid); |
|||
} |
|||
else if (genericType == typeof(DomainId)) |
|||
{ |
|||
converter = v => NamedId<DomainId>.Parse(v, ParserDomainId); |
|||
} |
|||
else if (genericType == typeof(string)) |
|||
{ |
|||
converter = v => NamedId<string>.Parse(v, ParserString); |
|||
} |
|||
else if (genericType == typeof(long)) |
|||
{ |
|||
converter = v => NamedId<long>.Parse(v, ParserLong); |
|||
} |
|||
} |
|||
|
|||
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) |
|||
{ |
|||
return sourceType == typeof(string); |
|||
} |
|||
|
|||
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) |
|||
{ |
|||
return destinationType == typeof(string); |
|||
} |
|||
|
|||
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) |
|||
{ |
|||
if (converter == null) |
|||
{ |
|||
throw new NotSupportedException(); |
|||
} |
|||
|
|||
return converter((string)value); |
|||
} |
|||
|
|||
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) |
|||
{ |
|||
return value.ToString()!; |
|||
} |
|||
|
|||
private static bool ParseDomainId(ReadOnlySpan<char> value, out DomainId result) |
|||
{ |
|||
result = DomainId.Create(new string(value)); |
|||
|
|||
return true; |
|||
} |
|||
|
|||
private static bool ParseString(ReadOnlySpan<char> value, out string result) |
|||
{ |
|||
result = new string(value); |
|||
|
|||
return true; |
|||
} |
|||
} |
|||
} |
|||
@ -1,165 +0,0 @@ |
|||
// ==========================================================================
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex UG (haftungsbeschraenkt)
|
|||
// All rights reserved. Licensed under the MIT license.
|
|||
// ==========================================================================
|
|||
|
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using Newtonsoft.Json; |
|||
using Squidex.Infrastructure.Json.Newtonsoft; |
|||
using Squidex.Infrastructure.Json.Objects; |
|||
using Squidex.Infrastructure.Validation; |
|||
|
|||
namespace Squidex.Infrastructure.Queries.Json |
|||
{ |
|||
public sealed class FilterConverter : JsonClassConverter<FilterNode<IJsonValue>> |
|||
{ |
|||
public override IEnumerable<Type> SupportedTypes |
|||
{ |
|||
get |
|||
{ |
|||
yield return typeof(CompareFilter<IJsonValue>); |
|||
yield return typeof(FilterNode<IJsonValue>); |
|||
yield return typeof(LogicalFilter<IJsonValue>); |
|||
yield return typeof(NegateFilter<IJsonValue>); |
|||
} |
|||
} |
|||
|
|||
public override bool CanConvert(Type objectType) |
|||
{ |
|||
return SupportedTypes.Contains(objectType); |
|||
} |
|||
|
|||
protected override FilterNode<IJsonValue> ReadValue(JsonReader reader, Type objectType, JsonSerializer serializer) |
|||
{ |
|||
if (reader.TokenType != JsonToken.StartObject) |
|||
{ |
|||
throw new JsonException($"Expected StartObject, but got {reader.TokenType}."); |
|||
} |
|||
|
|||
FilterNode<IJsonValue>? result = null; |
|||
|
|||
PropertyPath? comparePath = null; |
|||
|
|||
var compareOperator = (CompareOperator)99; |
|||
|
|||
IJsonValue? compareValue = null; |
|||
|
|||
while (reader.Read()) |
|||
{ |
|||
switch (reader.TokenType) |
|||
{ |
|||
case JsonToken.PropertyName: |
|||
var propertyName = reader.Value!.ToString()!; |
|||
|
|||
if (!reader.Read()) |
|||
{ |
|||
throw new JsonSerializationException("Unexpected end when reading filter."); |
|||
} |
|||
|
|||
if (result != null) |
|||
{ |
|||
throw new JsonSerializationException($"Unexpected property {propertyName}"); |
|||
} |
|||
|
|||
switch (propertyName.ToLowerInvariant()) |
|||
{ |
|||
case "not": |
|||
var filter = serializer.Deserialize<FilterNode<IJsonValue>>(reader)!; |
|||
|
|||
result = new NegateFilter<IJsonValue>(filter); |
|||
break; |
|||
case "and": |
|||
var andFilters = serializer.Deserialize<List<FilterNode<IJsonValue>>>(reader)!; |
|||
|
|||
result = new LogicalFilter<IJsonValue>(LogicalFilterType.And, andFilters); |
|||
break; |
|||
case "or": |
|||
var orFilters = serializer.Deserialize<List<FilterNode<IJsonValue>>>(reader)!; |
|||
|
|||
result = new LogicalFilter<IJsonValue>(LogicalFilterType.Or, orFilters); |
|||
break; |
|||
case "path": |
|||
comparePath = serializer.Deserialize<PropertyPath>(reader); |
|||
break; |
|||
case "op": |
|||
compareOperator = ReadOperator(reader, serializer); |
|||
break; |
|||
case "value": |
|||
compareValue = serializer.Deserialize<IJsonValue>(reader); |
|||
break; |
|||
} |
|||
|
|||
break; |
|||
case JsonToken.Comment: |
|||
break; |
|||
case JsonToken.EndObject: |
|||
if (result != null) |
|||
{ |
|||
return result; |
|||
} |
|||
|
|||
if (comparePath == null) |
|||
{ |
|||
throw new JsonSerializationException("Path not defined."); |
|||
} |
|||
|
|||
if (compareValue == null && compareOperator != CompareOperator.Empty) |
|||
{ |
|||
throw new JsonSerializationException("Value not defined."); |
|||
} |
|||
|
|||
if (!compareOperator.IsEnumValue()) |
|||
{ |
|||
throw new JsonSerializationException("Operator not defined."); |
|||
} |
|||
|
|||
return new CompareFilter<IJsonValue>(comparePath, compareOperator, compareValue ?? JsonValue.Null); |
|||
} |
|||
} |
|||
|
|||
throw new JsonSerializationException("Unexpected end when reading filter."); |
|||
} |
|||
|
|||
private static CompareOperator ReadOperator(JsonReader reader, JsonSerializer serializer) |
|||
{ |
|||
var value = serializer.Deserialize<string>(reader)!; |
|||
|
|||
switch (value.ToLowerInvariant()) |
|||
{ |
|||
case "eq": |
|||
return CompareOperator.Equals; |
|||
case "ne": |
|||
return CompareOperator.NotEquals; |
|||
case "lt": |
|||
return CompareOperator.LessThan; |
|||
case "le": |
|||
return CompareOperator.LessThanOrEqual; |
|||
case "gt": |
|||
return CompareOperator.GreaterThan; |
|||
case "ge": |
|||
return CompareOperator.GreaterThanOrEqual; |
|||
case "empty": |
|||
return CompareOperator.Empty; |
|||
case "contains": |
|||
return CompareOperator.Contains; |
|||
case "endswith": |
|||
return CompareOperator.EndsWith; |
|||
case "startswith": |
|||
return CompareOperator.StartsWith; |
|||
case "in": |
|||
return CompareOperator.In; |
|||
} |
|||
|
|||
throw new JsonSerializationException($"Unexpected compare operator, got {value}."); |
|||
} |
|||
|
|||
protected override void WriteValue(JsonWriter writer, FilterNode<IJsonValue> value, JsonSerializer serializer) |
|||
{ |
|||
throw new NotSupportedException(); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,91 @@ |
|||
// ==========================================================================
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex UG (haftungsbeschraenkt)
|
|||
// All rights reserved. Licensed under the MIT license.
|
|||
// ==========================================================================
|
|||
|
|||
using System; |
|||
using Squidex.Infrastructure.Json; |
|||
using Squidex.Infrastructure.Json.Objects; |
|||
|
|||
namespace Squidex.Infrastructure.Queries |
|||
{ |
|||
public sealed class JsonFilterSurrogate : ISurrogate<FilterNode<IJsonValue>> |
|||
{ |
|||
public FilterNode<IJsonValue>[]? And { get; set; } |
|||
|
|||
public FilterNode<IJsonValue>[]? Or { get; set; } |
|||
|
|||
public FilterNode<IJsonValue>? Not { get; set; } |
|||
|
|||
public string? Op { get; set; } |
|||
|
|||
public string? Path { get; set; } |
|||
|
|||
public IJsonValue? Value { get; set; } |
|||
|
|||
public void FromSource(FilterNode<IJsonValue> source) |
|||
{ |
|||
throw new NotSupportedException(); |
|||
} |
|||
|
|||
public FilterNode<IJsonValue> ToSource() |
|||
{ |
|||
if (Not != null) |
|||
{ |
|||
return new NegateFilter<IJsonValue>(Not); |
|||
} |
|||
|
|||
if (And != null) |
|||
{ |
|||
return new LogicalFilter<IJsonValue>(LogicalFilterType.And, And); |
|||
} |
|||
|
|||
if (Or != null) |
|||
{ |
|||
return new LogicalFilter<IJsonValue>(LogicalFilterType.Or, Or); |
|||
} |
|||
|
|||
if (!string.IsNullOrWhiteSpace(Path) && !string.IsNullOrWhiteSpace(Op)) |
|||
{ |
|||
var @operator = ReadOperator(Op); |
|||
|
|||
return new CompareFilter<IJsonValue>(Path, @operator, Value ?? JsonValue.Null); |
|||
} |
|||
|
|||
throw new JsonException("Invalid query."); |
|||
} |
|||
|
|||
private static CompareOperator ReadOperator(string op) |
|||
{ |
|||
switch (op.ToLowerInvariant()) |
|||
{ |
|||
case "eq": |
|||
return CompareOperator.Equals; |
|||
case "ne": |
|||
return CompareOperator.NotEquals; |
|||
case "lt": |
|||
return CompareOperator.LessThan; |
|||
case "le": |
|||
return CompareOperator.LessThanOrEqual; |
|||
case "gt": |
|||
return CompareOperator.GreaterThan; |
|||
case "ge": |
|||
return CompareOperator.GreaterThanOrEqual; |
|||
case "empty": |
|||
return CompareOperator.Empty; |
|||
case "contains": |
|||
return CompareOperator.Contains; |
|||
case "endswith": |
|||
return CompareOperator.EndsWith; |
|||
case "startswith": |
|||
return CompareOperator.StartsWith; |
|||
case "in": |
|||
return CompareOperator.In; |
|||
} |
|||
|
|||
throw new JsonException($"Unexpected compare operator, got {op}."); |
|||
} |
|||
} |
|||
} |
|||
@ -1,29 +0,0 @@ |
|||
// ==========================================================================
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex UG (haftungsbeschraenkt)
|
|||
// All rights reserved. Licensed under the MIT license.
|
|||
// ==========================================================================
|
|||
|
|||
using System; |
|||
using System.Linq; |
|||
using Newtonsoft.Json; |
|||
using Squidex.Infrastructure.Json.Newtonsoft; |
|||
|
|||
namespace Squidex.Infrastructure.Queries.Json |
|||
{ |
|||
public sealed class PropertyPathConverter : JsonClassConverter<PropertyPath> |
|||
{ |
|||
protected override PropertyPath ReadValue(JsonReader reader, Type objectType, JsonSerializer serializer) |
|||
{ |
|||
var value = serializer.Deserialize<string>(reader)!; |
|||
|
|||
return value; |
|||
} |
|||
|
|||
protected override void WriteValue(JsonWriter writer, PropertyPath value, JsonSerializer serializer) |
|||
{ |
|||
serializer.Serialize(writer, value.ToList()); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,36 @@ |
|||
// ==========================================================================
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex UG (haftungsbeschraenkt)
|
|||
// All rights reserved. Licensed under the MIT license.
|
|||
// ==========================================================================
|
|||
|
|||
using System; |
|||
using System.ComponentModel; |
|||
using System.Globalization; |
|||
|
|||
namespace Squidex.Infrastructure |
|||
{ |
|||
public sealed class RefTokenTypeConverter : TypeConverter |
|||
{ |
|||
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) |
|||
{ |
|||
return sourceType == typeof(string); |
|||
} |
|||
|
|||
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) |
|||
{ |
|||
return destinationType == typeof(string); |
|||
} |
|||
|
|||
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) |
|||
{ |
|||
return RefToken.Parse((string)value); |
|||
} |
|||
|
|||
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) |
|||
{ |
|||
return value.ToString()!; |
|||
} |
|||
} |
|||
} |
|||
Some files were not shown because too many files changed in this diff
Loading…
Reference in new issue