public static FeedRangeInternal ReadJObject(
            JObject jObject,
            JsonSerializer serializer)
        {
            if (jObject.TryGetValue(FeedRangeInternalConverter.RangePropertyName, out JToken rangeJToken))
            {
                try
                {
                    Documents.Routing.Range <string> completeRange = (Documents.Routing.Range <string>)rangeJsonConverter.ReadJson(rangeJToken.CreateReader(), typeof(Documents.Routing.Range <string>), null, serializer);
                    return(new FeedRangeEpk(completeRange));
                }
                catch (JsonSerializationException)
                {
                    throw new JsonReaderException();
                }
            }

            if (jObject.TryGetValue(FeedRangeInternalConverter.PartitionKeyPropertyName, out JToken pkJToken))
            {
                if (!PartitionKey.TryParseJsonString(pkJToken.Value <string>(), out PartitionKey partitionKey))
                {
                    throw new JsonReaderException();
                }

                return(new FeedRangePartitionKey(partitionKey));
            }

            if (jObject.TryGetValue(FeedRangeInternalConverter.PartitionKeyRangeIdPropertyName, out JToken pkRangeJToken))
            {
                return(new FeedRangePartitionKeyRange(pkRangeJToken.Value <string>()));
            }

            throw new JsonReaderException();
        }
        public static FeedRangePartitionKey ReadJObject(
            JObject jObject,
            JsonSerializer serializer)
        {
            if (!jObject.TryGetValue(FeedRangePartitionKeyConverter.PartitionKeyPropertyName, out JToken pkJToken))
            {
                throw new JsonReaderException();
            }

            if (!PartitionKey.TryParseJsonString(pkJToken.Value <string>(), out PartitionKey partitionKey))
            {
                throw new JsonReaderException();
            }

            return(new FeedRangePartitionKey(partitionKey));
        }
        public override object ReadJson(
            JsonReader reader,
            Type objectType,
            object existingValue,
            JsonSerializer serializer)
        {
            if (reader.TokenType == JsonToken.Null)
            {
                return(null);
            }

            if (reader.TokenType != JsonToken.StartObject)
            {
                throw new JsonSerializationException(ClientResources.FeedToken_UnknownFormat);
            }

            JObject jObject = JObject.Load(reader);

            if (!jObject.TryGetValue(FeedTokenInternalConverter.TypePropertyName, out JToken typeJtoken) ||
                !Enum.TryParse(typeJtoken.Value <int>().ToString(), ignoreCase: true, out FeedTokenType feedTokenType))
            {
                throw new JsonSerializationException(ClientResources.FeedToken_UnknownFormat);
            }

            switch (feedTokenType)
            {
            case FeedTokenType.EPKRange:
            {
                if (!jObject.TryGetValue(FeedTokenInternalConverter.RidPropertyName, out JToken ridJToken) ||
                    string.IsNullOrEmpty(ridJToken.Value <string>()))
                {
                    throw new JsonSerializationException(ClientResources.FeedToken_UnknownFormat);
                }

                if (!jObject.TryGetValue(FeedTokenInternalConverter.ContinuationPropertyName, out JToken continuationJToken))
                {
                    throw new JsonSerializationException(ClientResources.FeedToken_UnknownFormat);
                }

                if (!jObject.TryGetValue(FeedTokenInternalConverter.RangePropertyName, out JToken rangeJToken))
                {
                    throw new JsonSerializationException(ClientResources.FeedToken_UnknownFormat);
                }

                List <CompositeContinuationToken> ranges        = serializer.Deserialize <List <CompositeContinuationToken> >(continuationJToken.CreateReader());
                Documents.Routing.Range <string>  completeRange = serializer.Deserialize <Documents.Routing.Range <string> >(rangeJToken.CreateReader());
                return(new FeedTokenEPKRange(ridJToken.Value <string>(), completeRange, ranges));
            }

            case FeedTokenType.PartitionKeyValue:
            {
                if (!jObject.TryGetValue(FeedTokenInternalConverter.ContinuationPropertyName, out JToken continuationJToken))
                {
                    throw new JsonSerializationException(ClientResources.FeedToken_UnknownFormat);
                }

                if (!jObject.TryGetValue(FeedTokenInternalConverter.PartitionKeyPropertyName, out JToken pkJToken))
                {
                    throw new JsonSerializationException(ClientResources.FeedToken_UnknownFormat);
                }

                if (!PartitionKey.TryParseJsonString(pkJToken.Value <string>(), out PartitionKey partitionKey))
                {
                    throw new JsonSerializationException(ClientResources.FeedToken_UnknownFormat);
                }

                FeedTokenPartitionKey feedTokenPartitionKey = new FeedTokenPartitionKey(partitionKey);
                feedTokenPartitionKey.UpdateContinuation(continuationJToken.Value <string>());
                return(feedTokenPartitionKey);
            }

            case FeedTokenType.PartitionKeyRangeId:
            {
                if (!jObject.TryGetValue(FeedTokenInternalConverter.ContinuationPropertyName, out JToken continuationJToken))
                {
                    throw new JsonSerializationException(ClientResources.FeedToken_UnknownFormat);
                }

                if (!jObject.TryGetValue(FeedTokenInternalConverter.PartitionKeyRangeIdPropertyName, out JToken pkJToken))
                {
                    throw new JsonSerializationException(ClientResources.FeedToken_UnknownFormat);
                }

                FeedTokenPartitionKeyRange feedTokenPartitionKeyRange = new FeedTokenPartitionKeyRange(pkJToken.Value <string>());
                feedTokenPartitionKeyRange.UpdateContinuation(continuationJToken.Value <string>());
                return(feedTokenPartitionKeyRange);
            }
            }

            throw new JsonSerializationException(ClientResources.FeedToken_UnknownFormat);
        }
Esempio n. 4
0
        public static TryCatch <FeedRangeInternal> MonadicCreateFromCosmosElement(CosmosElement cosmosElement)
        {
            if (cosmosElement == null)
            {
                throw new ArgumentNullException(nameof(cosmosElement));
            }

            if (!(cosmosElement is CosmosObject cosmosObject))
            {
                return(TryCatch <FeedRangeInternal> .FromException(
                           new FormatException($"Expected object for feed range: {cosmosElement}.")));
            }

            if (!cosmosObject.TryGetValue(TypePropertyName, out CosmosString typeProperty))
            {
                return(TryCatch <FeedRangeInternal> .FromException(
                           new FormatException($"expected string type property for feed range: {cosmosElement}.")));
            }

            if (!cosmosObject.TryGetValue(ValuePropertyName, out CosmosElement valueProperty))
            {
                return(TryCatch <FeedRangeInternal> .FromException(
                           new FormatException($"expected value property for feed range: {cosmosElement}.")));
            }

            FeedRangeInternal feedRange;

            switch (typeProperty.Value)
            {
            case LogicalPartitionKey:
            {
                if (!(valueProperty is CosmosString stringValueProperty))
                {
                    return(TryCatch <FeedRangeInternal> .FromException(
                               new FormatException($"expected string value property for logical partition key feed range: {cosmosElement}.")));
                }

                if (!PartitionKey.TryParseJsonString(stringValueProperty.Value, out PartitionKey partitionKey))
                {
                    return(TryCatch <FeedRangeInternal> .FromException(
                               new FormatException($"failed to parse logical partition key value: {stringValueProperty.Value}.")));
                }

                feedRange = new FeedRangePartitionKey(partitionKey);
            }
            break;

            case PhysicalPartitionKeyRangeId:
            {
                if (!(valueProperty is CosmosString stringValueProperty))
                {
                    return(TryCatch <FeedRangeInternal> .FromException(
                               new FormatException($"expected string value property for physical partition key feed range: {cosmosElement}.")));
                }

                feedRange = new FeedRangePartitionKeyRange(stringValueProperty.Value);
            }
            break;

            case EffectivePartitionKeyRange:
            {
                if (!(valueProperty is CosmosObject objectValueProperty))
                {
                    return(TryCatch <FeedRangeInternal> .FromException(
                               new FormatException($"expected object value property for effective partition key range feed range: {cosmosElement}.")));
                }

                if (!objectValueProperty.TryGetValue(MinPropertyName, out CosmosString minPartitionKeyValue))
                {
                    return(TryCatch <FeedRangeInternal> .FromException(
                               new FormatException($"expected string value property for min effective partition key value: {cosmosElement}.")));
                }

                if (!objectValueProperty.TryGetValue(MaxPropertyName, out CosmosString maxPartitionKeyValue))
                {
                    return(TryCatch <FeedRangeInternal> .FromException(
                               new FormatException($"expected string value property for max effective partition key value: {cosmosElement}.")));
                }

                feedRange = new FeedRangeEpk(
                    new Documents.Routing.Range <string>(
                        min: minPartitionKeyValue.Value,
                        max: maxPartitionKeyValue.Value,
                        isMinInclusive: true,
                        isMaxInclusive: false));
            }
            break;

            default:
                throw new InvalidOperationException($"unexpected feed range type: {typeProperty.Value}");
            }

            return(TryCatch <FeedRangeInternal> .FromResult(feedRange));
        }