public override void Read(ContentSchemaField schemaField, ArrayField contentField, BsonValue bsonvalue) { if (bsonvalue is BsonArray bsonArray) { ArrayFieldOptions arrayOptions = (ArrayFieldOptions)schemaField.Options; foreach (BsonDocument item in bsonArray) { ArrayFieldItem arrayFieldItem = arrayOptions.CreateArrayField(); foreach (BsonElement subitem in item) { subitem.Value.FromBsonValue(subitem.Name, arrayFieldItem, arrayOptions); } contentField.Items.Add(arrayFieldItem); } } }
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; } } }