Esempio n. 1
0
        public async Task <IRelationshipObject> Deserialize(JsonReader reader, string currentPath)
        {
            if (reader.TokenType != JsonToken.StartObject)
            {
                throw new DeserializationException("Invalid relationship object", "Expected an object, but found " + reader.TokenType, currentPath);
            }

            IResourceLinkage linkage  = null;
            IMetadata        metadata = null;

            while (reader.Read())
            {
                if (reader.TokenType == JsonToken.EndObject)
                {
                    break;
                }

                var propertyName = (string)reader.Value;
                reader.Read();
                switch (propertyName)
                {
                case LinkageKeyName:
                    linkage = await _resourceLinkageFormatter.Deserialize(reader, currentPath + "/" + LinkageKeyName);

                    break;

                case MetaKeyName:
                    metadata = await _metadataFormatter.Deserialize(reader, currentPath + "/" + MetaKeyName);

                    break;
                }
            }

            return(new RelationshipObject(linkage, metadata));
        }
Esempio n. 2
0
        public async Task <IResourceCollectionDocument> Deserialize(JsonReader reader, string currentPath)
        {
            if (reader.TokenType != JsonToken.StartObject)
            {
                throw new JsonSerializationException("Document root is not an object!");
            }

            IResourceObject[] primaryData = null;
            IMetadata         metadata    = null;

            while (reader.Read())
            {
                if (reader.TokenType != JsonToken.PropertyName)
                {
                    break;
                }

                // Has to be a property name
                var propertyName = (string)reader.Value;
                reader.Read();

                switch (propertyName)
                {
                case RelatedDataKeyName:
                    // TODO: If we want to capture related resources, this would be the place to do it
                    reader.Skip();
                    break;

                case PrimaryDataKeyName:
                    primaryData = await DeserializePrimaryData(reader, currentPath + "/" + PrimaryDataKeyName);

                    break;

                case MetaKeyName:
                    metadata = await _metadataFormatter.Deserialize(reader, currentPath + "/" + MetaKeyName);

                    break;

                default:
                    reader.Skip();
                    break;
                }
            }

            return(new ResourceCollectionDocument(primaryData ?? new IResourceObject[] { }, new IResourceObject[] { }, metadata));
        }
        public async Task <IResourceObject> Deserialize(JsonReader reader, string currentPath)
        {
            if (reader.TokenType != JsonToken.StartObject)
            {
                throw new DeserializationException("Invalid resource object", "Expected an object.", currentPath);
            }

            string    type     = null;
            string    id       = null;
            IMetadata metadata = null;
            IDictionary <string, JToken> attributes = null;
            IDictionary <string, IRelationshipObject> relationships = null;

            while (reader.Read())
            {
                if (reader.TokenType == JsonToken.EndObject)
                {
                    break;
                }

                var propertyName = (string)reader.Value;
                reader.Read();

                switch (propertyName)
                {
                case TypeKeyName:
                    type = (string)reader.Value;
                    break;

                case IdKeyName:
                    id = (string)reader.Value;
                    break;

                case MetaKeyName:
                    metadata = await _metadataFormatter.Deserialize(reader, currentPath + "/" + MetaKeyName);

                    break;

                case AttributesKeyName:
                    attributes = DeserializeAttributes(reader, currentPath + "/" + AttributesKeyName);
                    break;

                case RelationshipsKeyName:
                    relationships = await DeserializeRelationships(reader, currentPath + "/" + RelationshipsKeyName);

                    break;

                default:
                    reader.Skip();
                    break;
                }
            }

            if (string.IsNullOrEmpty(type))
            {
                throw new DeserializationException("Resource object missing type", "Expected a value for `type`", currentPath + "/type");
            }

            return(new ResourceObject(type, id,
                                      attributes ?? new Dictionary <string, JToken>(),
                                      relationships ?? new Dictionary <string, IRelationshipObject>(),
                                      metadata: metadata));
        }