public static RestContentSchemaField ToRest(this ContentSchemaField schemaField)
        {
            RestContentSchemaField restContentFieldDefinition = new RestContentSchemaField();

            restContentFieldDefinition.Label     = schemaField.Label;
            restContentFieldDefinition.SortKey   = schemaField.SortKey;
            restContentFieldDefinition.FieldType = schemaField.FieldType;

            if (schemaField.Options != null)
            {
                restContentFieldDefinition.Options = JObject.FromObject(schemaField.Options, NewtonJsonExtensions.CreateSerializer());
            }

            return(restContentFieldDefinition);
        }
        public static ContentSchemaField ToModel(this RestContentSchemaField definition)
        {
            Type optionsType = ContentFieldManager.Default.GetOptionsType(definition.FieldType);

            ContentFieldOptions options = (ContentFieldOptions)definition.Options.ToObject(optionsType, NewtonJsonExtensions.CreateSerializer());

            ContentSchemaField schemaField = new ContentSchemaField(definition.FieldType, options);

            schemaField.Label   = definition.Label;
            schemaField.SortKey = definition.SortKey;

            return(schemaField);
        }
예제 #3
0
        public static void FromRestValue(this JToken bsonValue, string fieldName, IContentElement contentItem, IContentSchema schema)
        {
            if (contentItem.Fields.TryGetValue(fieldName, out ContentField contentField))
            {
                switch (bsonValue.Type)
                {
                case JTokenType.Null:
                    break;

                case JTokenType.String:
                    if (contentField is SingleValueContentField <string> stringField)
                    {
                        stringField.Value = bsonValue.Value <string>();
                    }
                    else if (contentField is SingleValueContentField <Guid?> guidField)
                    {
                        guidField.Value = Guid.Parse(bsonValue.Value <string>());
                    }
                    break;

                case JTokenType.Integer:
                    if (contentField is SingleValueContentField <short?> shortField)
                    {
                        shortField.Value = bsonValue.Value <short?>();
                    }
                    else if (contentField is SingleValueContentField <int?> intField)
                    {
                        intField.Value = bsonValue.Value <int?>();
                    }
                    else if (contentField is SingleValueContentField <long?> longField)
                    {
                        longField.Value = bsonValue.Value <long?>();
                    }
                    break;

                case JTokenType.Float:
                    ((SingleValueContentField <double?>)contentField).Value = bsonValue.Value <double>();
                    break;

                case JTokenType.Boolean:
                    ((SingleValueContentField <bool?>)contentField).Value = bsonValue.Value <bool>();
                    break;

                //case JTokenType.Guid:
                //    ((SingleContentField<Guid?>)contentField).Value = bsonValue.Value<Guid>();
                //    break;

                case JTokenType.Array:
                    if (contentField is ArrayField arrayField)
                    {
                        if (schema.Fields.TryGetValue(fieldName, out ContentSchemaField definition))
                        {
                            ArrayFieldOptions arrayOptions = definition.Options as ArrayFieldOptions;

                            foreach (JObject item in bsonValue)
                            {
                                ArrayFieldItem arrayFieldItem = arrayOptions.CreateArrayField();

                                foreach (var subitem in item)
                                {
                                    FromRestValue(subitem.Value, subitem.Key, arrayFieldItem, arrayOptions);
                                }

                                arrayField.Items.Add(arrayFieldItem);
                            }
                        }
                    }
                    break;

                case JTokenType.Object:
                    if (contentField is ReferenceField referenceField)
                    {
                        if (bsonValue.HasValues)
                        {
                            RestContentItem restContentItem = bsonValue.ToObject <RestContentItem>(NewtonJsonExtensions.CreateSerializer());
                            referenceField.ContentItem = restContentItem.ToModel();
                        }
                    }
                    else if (contentField is EmbedField embedField)
                    {
                        RestContentEmbedded restContentItem = bsonValue.ToObject <RestContentEmbedded>(NewtonJsonExtensions.CreateSerializer());
                        embedField.ContentEmbedded = restContentItem.ToModel();
                    }
                    else if (contentField is AssetField assetField)
                    {
                        if (bsonValue.HasValues)
                        {
                            RestAsset restAsset = bsonValue.ToObject <RestAsset>();
                            assetField.Asset = restAsset.ToModel();
                        }
                    }
                    else
                    {
                        ContentField c = (ContentField)bsonValue.ToObject(contentField.GetType(), NewtonJsonExtensions.CreateSerializer());

                        contentItem.Fields[fieldName] = c;
                    }
                    break;
                }
            }
        }
예제 #4
0
        public static JToken ToRestValue(this ContentField field, bool includeNavigationProperties)
        {
            JToken bsonValue = JValue.CreateNull();

            if (field is ISingleValueContentField singleValueField)
            {
                if (singleValueField.HasValue)
                {
                    bsonValue = new JValue(singleValueField.Value);
                }
            }
            else if (field is ReferenceField referenceField)
            {
                if (referenceField.ContentItem != null)
                {
                    if (includeNavigationProperties == true)
                    {
                        bsonValue = JObject.FromObject(referenceField.ContentItem.ToRest(true, false), NewtonJsonExtensions.CreateSerializer());
                    }
                }
            }
            else if (field is EmbedField embedField)
            {
                if (embedField.ContentEmbedded != null)
                {
                    bsonValue = JObject.FromObject(embedField.ContentEmbedded.ToRest(), NewtonJsonExtensions.CreateSerializer());
                }
            }
            else if (field is AssetField assetField)
            {
                if (assetField.Asset != null)
                {
                    bsonValue = JObject.FromObject(assetField.Asset.ToRest());
                }
            }
            else if (field is ArrayField arrayField)
            {
                JArray bsonArray = new JArray();

                foreach (ArrayFieldItem item in arrayField.Items)
                {
                    JObject doc = new JObject();

                    foreach (var f in item.Fields)
                    {
                        doc.Add(f.Key, f.Value.ToRestValue(includeNavigationProperties));
                    }

                    if (doc.Count > 0)
                    {
                        bsonArray.Add(doc);
                    }
                }

                if (bsonArray.Count > 0)
                {
                    bsonValue = bsonArray;
                }

                //arrayField.Items
            }
            else
            {
                //bsonValue = field.ToBsonDocument(field.GetType());
            }

            return(bsonValue);
        }
예제 #5
0
        public static ContentItem ToModel(this RestContentItem restContentItem, ContentSchema schema)
        {
            if (schema == null)
            {
                if (restContentItem.Schema.Type == JTokenType.String)
                {
                    schema = new ContentSchema(restContentItem.Schema.Value <string>());
                }
                else
                {
                    RestContentSchema restSchema = restContentItem.Schema.ToObject <RestContentSchema>(NewtonJsonExtensions.CreateSerializer());
                    schema = restSchema.ToModel();
                }
            }

            ContentItem contentItem = schema.CreateContentItem();

            contentItem.Id            = restContentItem.Id;
            contentItem.CreatedAt     = restContentItem.CreatedAt;
            contentItem.ModifiedAt    = restContentItem.ModifiedAt;
            contentItem.PublishedAt   = restContentItem.PublishedAt;
            contentItem.Version       = restContentItem.Version;
            contentItem.SchemaVersion = restContentItem.SchemaVersion;

            foreach (var restField in restContentItem.Fields)
            {
                restField.Value.FromRestValue(restField.Key, contentItem, schema);
            }

            return(contentItem);
        }
예제 #6
0
 public T Deserialize <T>(string json)
 {
     return(NewtonJsonExtensions.Deserialize <T>(json));
 }
예제 #7
0
 public string Serialize <T>(T obj)
 {
     return(NewtonJsonExtensions.Serialize(obj));
 }