Ejemplo n.º 1
0
        public static string ToJsonString <T>(this IJsonFormatter <T> formatter, T value, IJsonFormatterResolver formatterResolver)
        {
            var writer = new JsonWriter();

            formatter.Serialize(ref writer, value, formatterResolver);
            return(writer.ToString());
        }
Ejemplo n.º 2
0
        private IBucket GetKeyedBucket(ref JsonReader reader, IJsonFormatterResolver formatterResolver)
        {
            var token = reader.GetCurrentJsonToken();

            if (token == JsonToken.BeginObject)
            {
                return(GetCompositeBucket(ref reader, formatterResolver));
            }

            object key;

            if (token == JsonToken.String)
            {
                key = reader.ReadString();
            }
            else
            {
                var numberSegment = reader.ReadNumberSegment();
                if (numberSegment.IsLong())
                {
                    key = NumberConverter.ReadInt64(numberSegment.Array, numberSegment.Offset, out _);
                }
                else
                {
                    key = NumberConverter.ReadDouble(numberSegment.Array, numberSegment.Offset, out _);
                }
            }

            reader.ReadNext();             // ,
            var propertyName = reader.ReadPropertyName();

            if (propertyName == Parser.From || propertyName == Parser.To)
            {
                var rangeKey = key is double d
                                        ? d.ToString("#.#")
                                        : key.ToString();

                return(GetRangeBucket(ref reader, formatterResolver, rangeKey, propertyName));
            }

            string keyAsString = null;

            if (propertyName == Parser.KeyAsString)
            {
                keyAsString = reader.ReadString();
                reader.ReadNext();                 // ,
                reader.ReadNext();                 // "doc_count"
                reader.ReadNext();                 // :
            }

            var docCount = reader.ReadInt64();
            Dictionary <string, IAggregate> subAggregates = null;
            long?docCountErrorUpperBound = null;

            token = reader.GetCurrentJsonToken();
            if (token == JsonToken.ValueSeparator)
            {
                reader.ReadNext();
                propertyName = reader.ReadPropertyName();
                switch (propertyName)
                {
                case Parser.Score:
                    return(GetSignificantTermsBucket(ref reader, formatterResolver, key, docCount));

                case Parser.DocCountErrorUpperBound:
                {
                    docCountErrorUpperBound = reader.ReadNullableLong();
                    token = reader.GetCurrentJsonToken();
                    if (token == JsonToken.ValueSeparator)
                    {
                        reader.ReadNext();                                         // ,

                        propertyName  = reader.ReadPropertyName();
                        subAggregates = GetSubAggregates(ref reader, propertyName, formatterResolver);
                    }
                    break;
                }

                default:
                    subAggregates = GetSubAggregates(ref reader, propertyName, formatterResolver);
                    break;
                }
            }

            return(new KeyedBucket <object>(subAggregates)
            {
                Key = key,
                KeyAsString = keyAsString,
                DocCount = docCount,
                DocCountErrorUpperBound = docCountErrorUpperBound
            });
        }
Ejemplo n.º 3
0
 public void Serialize(ref JsonWriter writer, IAggregate value, IJsonFormatterResolver formatterResolver) => throw new NotSupportedException();
Ejemplo n.º 4
0
 public void Serialize(ref JsonWriter writer, ValueTuple <T1> value, IJsonFormatterResolver formatterResolver)
 {
     writer.WriteRaw(Cache[0]);
     formatterResolver.GetFormatterWithVerify <T1>().Serialize(ref writer, value.Item1, formatterResolver);
     writer.WriteEndObject();
 }
        public TInterface Deserialize(ref JsonReader reader, IJsonFormatterResolver formatterResolver)
        {
            var dictionary = DictionaryFormatter.Deserialize(ref reader, formatterResolver);

            return(typeof(TDictionary).CreateInstance <TDictionary>(dictionary));
        }
Ejemplo n.º 6
0
        public ILifecycleActions Deserialize(ref JsonReader reader, IJsonFormatterResolver formatterResolver)
        {
            if (reader.GetCurrentJsonToken() != JsonToken.BeginObject)
            {
                reader.ReadNextBlock();
                return(null);
            }

            var lifecycles = new Dictionary <string, ILifecycleAction>();
            var count      = 0;

            while (reader.ReadIsInObject(ref count))
            {
                var type = reader.ReadPropertyNameSegmentRaw();
                ILifecycleAction lifecycleAction = null;

                if (LifeCycleActions.TryGetValue(type, out var value))
                {
                    switch (value)
                    {
                    case 0:
                        lifecycleAction = formatterResolver.GetFormatter <AllocateLifecycleAction>()
                                          .Deserialize(ref reader, formatterResolver);
                        break;

                    case 1:
                        lifecycleAction = formatterResolver.GetFormatter <DeleteLifecycleAction>()
                                          .Deserialize(ref reader, formatterResolver);
                        break;

                    case 2:
                        lifecycleAction = formatterResolver.GetFormatter <ForceMergeLifecycleAction>()
                                          .Deserialize(ref reader, formatterResolver);
                        break;

                    case 3:
                        lifecycleAction = formatterResolver.GetFormatter <FreezeLifecycleAction>()
                                          .Deserialize(ref reader, formatterResolver);
                        break;

                    case 4:
                        lifecycleAction = formatterResolver.GetFormatter <ReadOnlyLifecycleAction>()
                                          .Deserialize(ref reader, formatterResolver);
                        break;

                    case 5:
                        lifecycleAction = formatterResolver.GetFormatter <RolloverLifecycleAction>()
                                          .Deserialize(ref reader, formatterResolver);
                        break;

                    case 6:
                        lifecycleAction = formatterResolver.GetFormatter <SetPriorityLifecycleAction>()
                                          .Deserialize(ref reader, formatterResolver);
                        break;

                    case 7:
                        lifecycleAction = formatterResolver.GetFormatter <ShrinkLifecycleAction>()
                                          .Deserialize(ref reader, formatterResolver);
                        break;

                    case 8:
                        lifecycleAction = formatterResolver.GetFormatter <UnfollowLifecycleAction>()
                                          .Deserialize(ref reader, formatterResolver);
                        break;

                    case 9:
                        lifecycleAction = formatterResolver.GetFormatter <WaitForSnapshotLifecycleAction>()
                                          .Deserialize(ref reader, formatterResolver);
                        break;
                    }

                    lifecycles.Add(type.Utf8String(), lifecycleAction);
                }
                else
                {
                    reader.ReadNextBlock();
                }
            }

            return(new LifecycleActions(lifecycles));
        }
Ejemplo n.º 7
0
 public void Serialize(ref JsonWriter writer, IAdjuster value, IJsonFormatterResolver formatterResolver)
 {
     JsonSerializer.NonGeneric.Serialize(value.GetType(), ref writer, value, formatterResolver);
 }
 public void Serialize(ref JsonWriter writer, T value, IJsonFormatterResolver formatterResolver) =>
 Write.Serialize(ref writer, value, formatterResolver);
        public object Deserialize(ref JsonReader reader, IJsonFormatterResolver formatterResolver)
        {
            var token = reader.GetCurrentJsonToken();

            switch (token)
            {
            case JsonToken.BeginObject:
            {
                var dict = new Dictionary <string, object>();
                reader.ReadIsBeginObjectWithVerify();
                var count = 0;
                while (!reader.ReadIsEndObjectWithSkipValueSeparator(ref count))
                {
                    var key   = reader.ReadPropertyName();
                    var value = Deserialize(ref reader, formatterResolver);
                    dict.Add(key, value);
                }
                return(dict);
            }

            case JsonToken.BeginArray:
            {
                var list = new List <object>(4);
                reader.ReadIsBeginArrayWithVerify();
                var count = 0;
                while (!reader.ReadIsEndArrayWithSkipValueSeparator(ref count))
                {
                    list.Add(Deserialize(ref reader, formatterResolver));
                }
                return(list);
            }

            case JsonToken.Number:
                var numberSegment = reader.ReadNumberSegment();
                // conditional operator here would cast both to double, so don't use.
                if (numberSegment.IsDouble())
                {
                    return(NumberConverter.ReadDouble(numberSegment.Array, numberSegment.Offset, out _));
                }

                // potential overflow if larger than int64
                return(NumberConverter.ReadInt64(numberSegment.Array, numberSegment.Offset, out _));

            case JsonToken.String:
                return(reader.ReadString());

            case JsonToken.True:
                return(reader.ReadBoolean());    // require advance

            case JsonToken.False:
                return(reader.ReadBoolean());    // require advance

            case JsonToken.ValueSeparator:
            case JsonToken.NameSeparator:
            case JsonToken.EndArray:
            case JsonToken.EndObject:
                throw new InvalidOperationException("Invalid Json Token:" + token);

            case JsonToken.Null:
                reader.ReadIsNull();
                return(null);

            case JsonToken.None:
            default:
                return(null);
            }
        }
Ejemplo n.º 10
0
        private static void Serialize <TProperty>(ref JsonWriter writer, TProperty value, IJsonFormatterResolver formatterResolver)
            where TProperty : class, IProperty
        {
            var formatter = formatterResolver.GetFormatter <TProperty>();

            formatter.Serialize(ref writer, value, formatterResolver);
        }
 public T Deserialize(ref JsonReader reader, IJsonFormatterResolver formatterResolver) =>
 Read.Deserialize(ref reader, formatterResolver);
Ejemplo n.º 12
0
        public IProperty Deserialize(ref JsonReader reader, IJsonFormatterResolver formatterResolver)
        {
            var arraySegment = reader.ReadNextBlockSegment();

            var    segmentReader = new JsonReader(arraySegment.Array, arraySegment.Offset);
            var    count         = 0;
            string typeString    = null;
            var    type          = FieldType.None;

            while (segmentReader.ReadIsInObject(ref count))
            {
                var propertyName = segmentReader.ReadPropertyNameSegmentRaw();
                if (AutomataDictionary.TryGetValue(propertyName, out var value))
                {
                    switch (value)
                    {
                    case 0:
                        typeString = segmentReader.ReadString();
                        type       = typeString.ToEnum <FieldType>().GetValueOrDefault(type);
                        break;

                    case 1:
                        if (type == FieldType.None)
                        {
                            type = FieldType.Object;
                        }

                        segmentReader.ReadNextBlock();
                        break;
                    }

                    // explicit type has been set
                    if (value == 0)
                    {
                        break;
                    }
                }
                else
                {
                    segmentReader.ReadNextBlock();
                }
            }

            segmentReader = new JsonReader(arraySegment.Array, arraySegment.Offset);

            switch (type)
            {
            case FieldType.Text: return(Deserialize <TextProperty>(ref segmentReader, formatterResolver));

            case FieldType.Keyword: return(Deserialize <KeywordProperty>(ref segmentReader, formatterResolver));

            case FieldType.SearchAsYouType: return(Deserialize <SearchAsYouTypeProperty>(ref segmentReader, formatterResolver));

            case FieldType.Float:
            case FieldType.Double:
            case FieldType.Byte:
            case FieldType.Short:
            case FieldType.Integer:
            case FieldType.Long:
            case FieldType.ScaledFloat:
            case FieldType.HalfFloat:
                var numberProperty = Deserialize <NumberProperty>(ref segmentReader, formatterResolver);
                ((IProperty)numberProperty).Type = typeString;
                return(numberProperty);

            case FieldType.Date: return(Deserialize <DateProperty>(ref segmentReader, formatterResolver));

            case FieldType.DateNanos: return(Deserialize <DateNanosProperty>(ref segmentReader, formatterResolver));

            case FieldType.Boolean: return(Deserialize <BooleanProperty>(ref segmentReader, formatterResolver));

            case FieldType.Binary: return(Deserialize <BinaryProperty>(ref segmentReader, formatterResolver));

            case FieldType.Object: return(Deserialize <ObjectProperty>(ref segmentReader, formatterResolver));

            case FieldType.Nested: return(Deserialize <NestedProperty>(ref segmentReader, formatterResolver));

            case FieldType.Ip: return(Deserialize <IpProperty>(ref segmentReader, formatterResolver));

            case FieldType.GeoPoint: return(Deserialize <GeoPointProperty>(ref segmentReader, formatterResolver));

            case FieldType.GeoShape: return(Deserialize <GeoShapeProperty>(ref segmentReader, formatterResolver));

            case FieldType.Shape: return(Deserialize <ShapeProperty>(ref segmentReader, formatterResolver));

            case FieldType.Completion: return(Deserialize <CompletionProperty>(ref segmentReader, formatterResolver));

            case FieldType.TokenCount: return(Deserialize <TokenCountProperty>(ref segmentReader, formatterResolver));

            case FieldType.Murmur3Hash: return(Deserialize <Murmur3HashProperty>(ref segmentReader, formatterResolver));

            case FieldType.Percolator: return(Deserialize <PercolatorProperty>(ref segmentReader, formatterResolver));

            case FieldType.DateRange: return(Deserialize <DateRangeProperty>(ref segmentReader, formatterResolver));

            case FieldType.DoubleRange: return(Deserialize <DoubleRangeProperty>(ref segmentReader, formatterResolver));

            case FieldType.FloatRange: return(Deserialize <FloatRangeProperty>(ref segmentReader, formatterResolver));

            case FieldType.IntegerRange: return(Deserialize <IntegerRangeProperty>(ref segmentReader, formatterResolver));

            case FieldType.LongRange: return(Deserialize <LongRangeProperty>(ref segmentReader, formatterResolver));

            case FieldType.IpRange: return(Deserialize <IpRangeProperty>(ref segmentReader, formatterResolver));

            case FieldType.Join: return(Deserialize <JoinProperty>(ref segmentReader, formatterResolver));

            case FieldType.Alias: return(Deserialize <FieldAliasProperty>(ref segmentReader, formatterResolver));

            case FieldType.RankFeature: return(Deserialize <RankFeatureProperty>(ref segmentReader, formatterResolver));

            case FieldType.RankFeatures: return(Deserialize <RankFeaturesProperty>(ref segmentReader, formatterResolver));

            case FieldType.Flattened: return(Deserialize <FlattenedProperty>(ref segmentReader, formatterResolver));

            case FieldType.Histogram: return(Deserialize <HistogramProperty>(ref segmentReader, formatterResolver));

            case FieldType.None:
                // no "type" field in the property mapping, or FieldType enum could not be parsed from typeString
                return(Deserialize <ObjectProperty>(ref segmentReader, formatterResolver));

            default:
                throw new ArgumentOutOfRangeException(nameof(type), type, "mapping property converter does not know this value");
            }
        }
Ejemplo n.º 13
0
        public void Serialize(ref JsonWriter writer, IProperty value, IJsonFormatterResolver formatterResolver)
        {
            if (value == null)
            {
                writer.WriteNull();
                return;
            }

            switch (value)
            {
            case ITextProperty textProperty:
                Serialize(ref writer, textProperty, formatterResolver);
                break;

            case IKeywordProperty keywordProperty:
                Serialize(ref writer, keywordProperty, formatterResolver);
                break;

            case INumberProperty numberProperty:
                Serialize(ref writer, numberProperty, formatterResolver);
                break;

            case IDateProperty dateProperty:
                Serialize(ref writer, dateProperty, formatterResolver);
                break;

            case IBooleanProperty booleanProperty:
                Serialize(ref writer, booleanProperty, formatterResolver);
                break;

            case INestedProperty nestedProperty:
                Serialize(ref writer, nestedProperty, formatterResolver);
                break;

            case IObjectProperty objectProperty:
                Serialize(ref writer, objectProperty, formatterResolver);
                break;

            case ISearchAsYouTypeProperty searchAsYouTypeProperty:
                Serialize(ref writer, searchAsYouTypeProperty, formatterResolver);
                break;

            case IDateNanosProperty dateNanosProperty:
                Serialize(ref writer, dateNanosProperty, formatterResolver);
                break;

            case IBinaryProperty binaryProperty:
                Serialize(ref writer, binaryProperty, formatterResolver);
                break;

            case IIpProperty ipProperty:
                Serialize(ref writer, ipProperty, formatterResolver);
                break;

            case IGeoPointProperty geoPointProperty:
                Serialize(ref writer, geoPointProperty, formatterResolver);
                break;

            case IGeoShapeProperty geoShapeProperty:
                Serialize(ref writer, geoShapeProperty, formatterResolver);
                break;

            case IShapeProperty shapeProperty:
                Serialize(ref writer, shapeProperty, formatterResolver);
                break;

            case ICompletionProperty completionProperty:
                Serialize(ref writer, completionProperty, formatterResolver);
                break;

            case ITokenCountProperty tokenCountProperty:
                Serialize(ref writer, tokenCountProperty, formatterResolver);
                break;

            case IMurmur3HashProperty murmur3HashProperty:
                Serialize(ref writer, murmur3HashProperty, formatterResolver);
                break;

            case IPercolatorProperty percolatorProperty:
                Serialize(ref writer, percolatorProperty, formatterResolver);
                break;

            case IDateRangeProperty dateRangeProperty:
                Serialize(ref writer, dateRangeProperty, formatterResolver);
                break;

            case IDoubleRangeProperty doubleRangeProperty:
                Serialize(ref writer, doubleRangeProperty, formatterResolver);
                break;

            case IFloatRangeProperty floatRangeProperty:
                Serialize(ref writer, floatRangeProperty, formatterResolver);
                break;

            case IIntegerRangeProperty integerRangeProperty:
                Serialize(ref writer, integerRangeProperty, formatterResolver);
                break;

            case ILongRangeProperty longRangeProperty:
                Serialize(ref writer, longRangeProperty, formatterResolver);
                break;

            case IIpRangeProperty ipRangeProperty:
                Serialize(ref writer, ipRangeProperty, formatterResolver);
                break;

            case IJoinProperty joinProperty:
                Serialize(ref writer, joinProperty, formatterResolver);
                break;

            case IFieldAliasProperty fieldAliasProperty:
                Serialize(ref writer, fieldAliasProperty, formatterResolver);
                break;

            case IRankFeatureProperty rankFeatureProperty:
                Serialize(ref writer, rankFeatureProperty, formatterResolver);
                break;

            case IRankFeaturesProperty rankFeaturesProperty:
                Serialize(ref writer, rankFeaturesProperty, formatterResolver);
                break;

            case IFlattenedProperty flattenedProperty:
                Serialize(ref writer, flattenedProperty, formatterResolver);
                break;

            case IHistogramProperty histogramProperty:
                Serialize(ref writer, histogramProperty, formatterResolver);
                break;

            case IGenericProperty genericProperty:
                Serialize(ref writer, genericProperty, formatterResolver);
                break;

            default:
                var formatter = formatterResolver.GetFormatter <object>();
                formatter.Serialize(ref writer, value, formatterResolver);
                break;
            }
        }
        private static void Serialize <TCommand>(ref JsonWriter writer, IClusterRerouteCommand value, IJsonFormatterResolver formatterResolver)
            where TCommand : class, IClusterRerouteCommand
        {
            var formatter = formatterResolver.GetFormatter <TCommand>();

            formatter.Serialize(ref writer, value as TCommand, formatterResolver);
        }
Ejemplo n.º 15
0
        public void Serialize(ref JsonWriter writer, ILifecycleActions value, IJsonFormatterResolver formatterResolver)
        {
            if (value == null)
            {
                writer.WriteNull();
                return;
            }

            writer.WriteBeginObject();
            var count = 0;

            foreach (var action in value)
            {
                if (count > 0)
                {
                    writer.WriteValueSeparator();
                }

                writer.WritePropertyName(action.Key);

                switch (action.Key)
                {
                case "allocate":
                    Serialize <IAllocateLifecycleAction>(ref writer, action.Value, formatterResolver);
                    break;

                case "delete":
                    Serialize <IDeleteLifecycleAction>(ref writer, action.Value, formatterResolver);
                    break;

                case "forcemerge":
                    Serialize <IForceMergeLifecycleAction>(ref writer, action.Value, formatterResolver);
                    break;

                case "freeze":
                    Serialize <IFreezeLifecycleAction>(ref writer, action.Value, formatterResolver);
                    break;

                case "readonly":
                    Serialize <IReadOnlyLifecycleAction>(ref writer, action.Value, formatterResolver);
                    break;

                case "rollover":
                    Serialize <IRolloverLifecycleAction>(ref writer, action.Value, formatterResolver);
                    break;

                case "set_priority":
                    Serialize <ISetPriorityLifecycleAction>(ref writer, action.Value, formatterResolver);
                    break;

                case "shrink":
                    Serialize <IShrinkLifecycleAction>(ref writer, action.Value, formatterResolver);
                    break;

                case "unfollow":
                    Serialize <IUnfollowLifecycleAction>(ref writer, action.Value, formatterResolver);
                    break;

                case "wait_for_snapshot":
                    Serialize <IWaitForSnapshotLifecycleAction>(ref writer, action.Value, formatterResolver);
                    break;

                default:
                    DynamicObjectResolver.ExcludeNullCamelCase.GetFormatter <ILifecycleAction>()
                    .Serialize(ref writer, action.Value, formatterResolver);
                    break;
                }

                count++;
            }
            writer.WriteEndObject();
        }
        public void Serialize(ref JsonWriter writer, object value, IJsonFormatterResolver formatterResolver)
        {
            if (value == null)
            {
                writer.WriteNull();
                return;
            }

            var t = value.GetType();

            int key;

            if (typeToJumpCode.TryGetValue(t, out key))
            {
                switch (key)
                {
                case 0: writer.WriteBoolean((bool)value); return;

                case 1: CharFormatter.Default.Serialize(ref writer, (char)value, formatterResolver); return;

                case 2: writer.WriteSByte((sbyte)value); return;

                case 3: writer.WriteByte((byte)value); return;

                case 4: writer.WriteInt16((Int16)value); return;

                case 5: writer.WriteUInt16((UInt16)value); return;

                case 6: writer.WriteInt32((int)value); return;

                case 7: writer.WriteUInt32((UInt32)value); return;

                case 8: writer.WriteInt64((long)value); return;

                case 9: writer.WriteUInt64((UInt64)value); return;

                case 10: writer.WriteSingle((float)value); return;

                case 11: writer.WriteDouble((double)value); return;

                case 12: ISO8601DateTimeFormatter.Default.Serialize(ref writer, (DateTime)value, formatterResolver); return;

                case 13: writer.WriteString((string)value); return;

                case 14: ByteArrayFormatter.Default.Serialize(ref writer, (byte[])value, formatterResolver); return;

                default:
                    break;
                }
            }

            if (t.IsEnum)
            {
                writer.WriteString(t.ToString()); // enum as stringq
                return;
            }

            var dict = value as IDictionary; // check dictionary first

            if (dict != null)
            {
                var count = 0;
                writer.WriteBeginObject();
                foreach (DictionaryEntry item in dict)
                {
                    if (count++ != 0)
                    {
                        writer.WriteValueSeparator();
                    }
                    writer.WritePropertyName((string)item.Key);
                    Serialize(ref writer, item.Value, formatterResolver);
                }
                writer.WriteEndObject();
                return;
            }

            var collection = value as ICollection;

            if (collection != null)
            {
                var count = 0;
                writer.WriteBeginArray();
                foreach (var item in collection)
                {
                    if (count++ != 0)
                    {
                        writer.WriteValueSeparator();
                    }
                    Serialize(ref writer, item, formatterResolver);
                }
                writer.WriteEndArray();
                return;
            }

            throw new InvalidOperationException("Not supported primitive object resolver. type:" + t.Name);
        }
Ejemplo n.º 17
0
 private void Serialize <TLifecycleAction>(ref JsonWriter writer, ILifecycleAction value, IJsonFormatterResolver formatterResolver)
     where TLifecycleAction : ILifecycleAction =>
 formatterResolver.GetFormatter <TLifecycleAction>().Serialize(ref writer, (TLifecycleAction)value, formatterResolver);
        public void Serialize(ref JsonWriter writer, IUpdateIndexSettingsRequest value, IJsonFormatterResolver formatterResolver)
        {
            if (value == null)
            {
                writer.WriteNull();
                return;
            }

            DynamicIndexSettingsFormatter.Serialize(ref writer, value.IndexSettings, formatterResolver);
        }
Ejemplo n.º 19
0
 public IPostJobDataRequest Deserialize(ref JsonReader reader, IJsonFormatterResolver formatterResolver) =>
 throw new NotSupportedException();
 public IMultiSearchTemplateRequest Deserialize(ref JsonReader reader, IJsonFormatterResolver formatterResolver) =>
 formatterResolver.GetFormatter <MultiSearchTemplateRequest>().Deserialize(ref reader, formatterResolver);
Ejemplo n.º 21
0
 public IMultiSearchRequest Deserialize(ref JsonReader reader, IJsonFormatterResolver formatterResolver) =>
 DynamicObjectResolver.ExcludeNullCamelCase.GetFormatter <IMultiSearchRequest>().Deserialize(ref reader, formatterResolver);
        public void Serialize(ref JsonWriter writer, IMultiSearchTemplateRequest value, IJsonFormatterResolver formatterResolver)
        {
            if (value?.Operations == null)
            {
                return;
            }

            var settings   = formatterResolver.GetConnectionSettings();
            var serializer = settings.RequestResponseSerializer;

            foreach (var operation in value.Operations.Values)
            {
                var p = operation.RequestParameters;

                string GetString(string key)
                {
                    return(p.GetResolvedQueryStringValue(key, settings));
                }

                IUrlParameter indices = value.Index == null || !value.Index.Equals(operation.Index)
                                        ? operation.Index
                                        : null;

                var searchType = GetString("search_type");
                if (searchType == "query_then_fetch")
                {
                    searchType = null;
                }

                var header = new
                {
                    index              = indices?.GetString(settings),
                    search_type        = searchType,
                    preference         = GetString("preference"),
                    routing            = GetString("routing"),
                    ignore_unavailable = GetString("ignore_unavailable")
                };

                writer.WriteSerialized(header, serializer, settings, SerializationFormatting.None);
                writer.WriteRaw(Newline);
                writer.WriteSerialized(operation, serializer, settings, SerializationFormatting.None);
                writer.WriteRaw(Newline);
            }
        }
 public void Serialize(ref JsonWriter writer, TInterface value, IJsonFormatterResolver formatterResolver) =>
 DictionaryFormatter.Serialize(ref writer, value, formatterResolver);
        public void Serialize(ref JsonWriter writer, GetRepositoryResponse value, IJsonFormatterResolver formatterResolver)
        {
            var formatter = DynamicObjectResolver.ExcludeNullCamelCase.GetFormatter <GetRepositoryResponse>();

            formatter.Serialize(ref writer, value, formatterResolver);
        }
Ejemplo n.º 25
0
 internal TopHitsAggregate(IList <LazyDocument> hits, IJsonFormatterResolver formatterResolver)
 {
     _hits = hits;
     _formatterResolver = formatterResolver;
 }
        public GetRepositoryResponse Deserialize(ref JsonReader reader, IJsonFormatterResolver formatterResolver)
        {
            var response     = new GetRepositoryResponse();
            var repositories = new Dictionary <string, ISnapshotRepository>();
            var count        = 0;

            while (reader.ReadIsInObject(ref count))
            {
                var property = reader.ReadPropertyNameSegmentRaw();
                if (ResponseFormatterHelpers.ServerErrorFields.TryGetValue(property, out var errorValue))
                {
                    switch (errorValue)
                    {
                    case 0:
                        if (reader.GetCurrentJsonToken() == JsonToken.String)
                        {
                            response.Error = new Error {
                                Reason = reader.ReadString()
                            }
                        }
                        ;
                        else
                        {
                            var formatter = formatterResolver.GetFormatter <Error>();
                            response.Error = formatter.Deserialize(ref reader, formatterResolver);
                        }
                        break;

                    case 1:
                        if (reader.GetCurrentJsonToken() == JsonToken.Number)
                        {
                            response.StatusCode = reader.ReadInt32();
                        }
                        else
                        {
                            reader.ReadNextBlock();
                        }
                        break;
                    }
                }
                else
                {
                    var name                  = property.Utf8String();
                    var snapshotSegment       = reader.ReadNextBlockSegment();
                    var snapshotSegmentReader = new JsonReader(snapshotSegment.Array, snapshotSegment.Offset);
                    var segmentCount          = 0;

                    string repositoryType        = null;
                    ArraySegment <byte> settings = default;

                    while (snapshotSegmentReader.ReadIsInObject(ref segmentCount))
                    {
                        var propertyName = snapshotSegmentReader.ReadPropertyName();
                        switch (propertyName)
                        {
                        case "type":
                            repositoryType = snapshotSegmentReader.ReadString();
                            break;

                        case "settings":
                            settings = snapshotSegmentReader.ReadNextBlockSegment();
                            break;

                        default:
                            snapshotSegmentReader.ReadNextBlock();
                            break;
                        }
                    }

                    switch (repositoryType)
                    {
                    case "fs":
                        var fs = GetRepository <FileSystemRepository, FileSystemRepositorySettings>(settings, formatterResolver);
                        repositories.Add(name, fs);
                        break;

                    case "url":
                        var url = GetRepository <ReadOnlyUrlRepository, ReadOnlyUrlRepositorySettings>(settings, formatterResolver);
                        repositories.Add(name, url);
                        break;

                    case "azure":
                        var azure = GetRepository <AzureRepository, AzureRepositorySettings>(settings, formatterResolver);
                        repositories.Add(name, azure);
                        break;

                    case "s3":
                        var s3 = GetRepository <S3Repository, S3RepositorySettings>(settings, formatterResolver);
                        repositories.Add(name, s3);
                        break;

                    case "hdfs":
                        var hdfs = GetRepository <HdfsRepository, HdfsRepositorySettings>(settings, formatterResolver);
                        repositories.Add(name, hdfs);
                        break;

                    case "source":
                        // reset the offset
                        snapshotSegmentReader.ResetOffset();
                        var source = formatterResolver.GetFormatter <ISourceOnlyRepository>()
                                     .Deserialize(ref snapshotSegmentReader, formatterResolver);
                        repositories.Add(name, source);
                        break;
                    }
                }
            }

            response.Repositories = repositories;
            return(response);
        }
Ejemplo n.º 27
0
 public IAggregate Deserialize(ref JsonReader reader, IJsonFormatterResolver formatterResolver) =>
 ReadAggregate(ref reader, formatterResolver);
        private TRepository GetRepository <TRepository, TSettings>(ArraySegment <byte> settings, IJsonFormatterResolver formatterResolver)
            where TRepository : ISnapshotRepository
            where TSettings : IRepositorySettings
        {
            if (settings == default)
            {
                return(typeof(TRepository).CreateInstance <TRepository>());
            }

            var formatter        = formatterResolver.GetFormatter <TSettings>();
            var reader           = new JsonReader(settings.Array, settings.Offset);
            var resolvedSettings = formatter.Deserialize(ref reader, formatterResolver);

            return(typeof(TRepository).CreateInstance <TRepository>(resolvedSettings));
        }
Ejemplo n.º 29
0
        private IAggregate ReadAggregate(ref JsonReader reader, IJsonFormatterResolver formatterResolver)
        {
            reader.ReadIsBeginObjectWithVerify();
            IAggregate aggregate = null;

            if (reader.ReadIsEndObject())
            {
                return(null);
            }

            var propertyName = reader.ReadPropertyNameSegmentRaw();
            Dictionary <string, object> meta = null;

            if (propertyName.EqualsBytes(MetaField))
            {
                meta = GetMetadata(ref reader, formatterResolver);
                reader.ReadIsValueSeparatorWithVerify();
                propertyName = reader.ReadPropertyNameSegmentRaw();
            }

            if (RootFields.TryGetValue(propertyName, out var value))
            {
                switch (value)
                {
                case 0:
                    aggregate = GetPercentilesAggregate(ref reader, meta);
                    break;

                case 1:
                    aggregate = GetValueAggregate(ref reader, formatterResolver, meta);
                    break;

                case 2:
                    var compositeKeyFormatter = formatterResolver.GetFormatter <CompositeKey>();
                    var afterKey = compositeKeyFormatter.Deserialize(ref reader, formatterResolver);
                    reader.ReadNext();                             // ,
                    propertyName = reader.ReadPropertyNameSegmentRaw();
                    var bucketAggregate = propertyName.EqualsBytes(BucketsField)
                                                        ? GetMultiBucketAggregate(ref reader, formatterResolver, ref propertyName, meta) as BucketAggregate ?? new BucketAggregate {
                        Meta = meta
                    }
                                                        : new BucketAggregate {
                        Meta = meta
                    };
                    bucketAggregate.AfterKey = afterKey;
                    aggregate = bucketAggregate;
                    break;

                case 3:
                case 4:
                    aggregate = GetMultiBucketAggregate(ref reader, formatterResolver, ref propertyName, meta);
                    break;

                case 5:
                    aggregate = GetStatsAggregate(ref reader, formatterResolver, meta);
                    break;

                case 6:
                    aggregate = GetSingleBucketAggregate(ref reader, formatterResolver, meta);
                    break;

                case 7:
                    aggregate = GetGeoBoundsAggregate(ref reader, formatterResolver, meta);
                    break;

                case 8:
                    aggregate = GetTopHitsAggregate(ref reader, formatterResolver, meta);
                    break;

                case 9:
                    aggregate = GetGeoCentroidAggregate(ref reader, formatterResolver, meta);
                    break;

                case 10:
                    aggregate = GetMatrixStatsAggregate(ref reader, formatterResolver, meta);
                    break;

                case 11:
                    aggregate = GetBoxplotAggregate(ref reader, meta);
                    break;

                case 12:
                    aggregate = GetTopMetricsAggregate(ref reader, formatterResolver, meta);
                    break;

                case 13:
                    aggregate = GetGeoLineAggregate(ref reader, formatterResolver, meta);
                    break;
                }
            }
            else
            {
                reader.ReadNextBlock();
            }

            reader.ReadIsEndObjectWithVerify();
            return(aggregate);
        }
Ejemplo n.º 30
0
        public void Serialize(ref JsonWriter writer, IGrouping <TKey, TElement> value, IJsonFormatterResolver formatterResolver)
        {
            if (value == null)
            {
                writer.WriteNull();
                return;
            }
            else
            {
                writer.WriteRaw(CollectionFormatterHelper.groupingName[0]);
                formatterResolver.GetFormatterWithVerify <TKey>().Serialize(ref writer, value.Key, formatterResolver);
                writer.WriteRaw(CollectionFormatterHelper.groupingName[1]);
                formatterResolver.GetFormatterWithVerify <IEnumerable <TElement> >().Serialize(ref writer, value.AsEnumerable(), formatterResolver);

                writer.WriteEndObject();
            }
        }