Ejemplo n.º 1
0
        public void SerializeDouble()
        {
            var poco = new
            {
                Whole      = 1d,
                Fractional = 1.1d
            };

            var serialized = _serializer.SerializeToString(poco);

            serialized.Should().Be("{\"whole\":1.0,\"fractional\":1.1}");
        }
Ejemplo n.º 2
0
        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            var v     = _builtInSerializer.SerializeToString(value);
            var token = JToken.Parse(v);

            writer.WriteToken(token.CreateReader(), true);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Allows a subclass to write out objects that have no configured literal writer.
        /// </summary>
        /// <param name="value">The value to be written as a json construct</param>
        /// <param name="output">The writer to write on</param>
        protected override void WriteLiteralValue(object value, TextWriter output)
        {
            if (_serializer != null)
            {
                string jsonString = _serializer.SerializeToString(value, SerializationFormatting.None);
                output.Write(jsonString);
                return;
            }

            base.WriteLiteralValue(value, output);
        }
Ejemplo n.º 4
0
        /**
         * ==== Constructor
         *
         * Sometimes, the constructor of `Union<TFirst,TSecond>` may be required in cases where the compiler is
         * unable to infer the correct implicit conversion
         */
        public void Constructor()
        {
            var sourceFilterTrue = new Union <bool, ISourceFilter>(true);

            var sourceFilterInterface = new Union <bool, ISourceFilter>(new SourceFilter
            {
                Includes = new [] { "foo.*" }
            });

            /**
             * ==== Match
             *
             * The `Match` method can be used to operate on the value encapsulated by the instance of `Union<TFirst,TSecond>`.
             * Two delegates are passed; one to operate on a `TFirst` value and the other to operate on a `TSecond` value.
             */
            sourceFilterTrue.Match(
                b => b.Should().BeTrue(),
                s => s.Should().BeNull());

            sourceFilterInterface.Match(
                b => b.Should().BeFalse(),
                s => s.Should().NotBeNull());

            /**
             * The delegates can also return a value
             */
            var serializedFilterTrue = sourceFilterTrue.Match(
                b => serializer.SerializeToString(b),
                s => null);

            serializedFilterTrue.Should().Be("true");

            var serializedFilterInterface = sourceFilterTrue.Match(
                b => null,
                s => serializer.SerializeToString(s));

            serializedFilterInterface.Should().Be("{\"includes\":[\"foo.*\"]}");
        }
Ejemplo n.º 5
0
        private static JObject GetExplanationObj <T>(IHit <T> hit, IElasticsearchSerializer serializer) where T : TreeRecord
        {
            if (hit?.Explanation?.Value == null)
            {
                return(null);
            }

            var explanationsObj = new JObject
            {
                { "value", hit.Explanation.Value },
                { "explanation", GetExplanationWithObscuredValues(serializer.SerializeToString(hit.Explanation)) }
            };

            return(explanationsObj);
        }
Ejemplo n.º 6
0
        private void ProcessQueryResult <T>(ElasticQueryResult <T> result, FacetFilters[] facetsFilters, UserAccess access,
                                            IElasticsearchSerializer serializer) where T : TreeRecord
        {
            var response = result.Response;

            var hits = response?.Hits ?? new List <IHit <T> >();

            result.TotalNumberOfHits = response?.HitsMetadata != null ? (int)response.HitsMetadata.Total.Value : -1;
            var entries = new List <Entity <T> >();

            foreach (var hit in hits)
            {
                var data = JsonConvert.DeserializeObject <T>(serializer.SerializeToString(hit.Source));

                var entry = new Entity <T>
                {
                    Data        = data,
                    Highlight   = GetHighlightingObj(hit, access),
                    Explanation = GetExplanationObj(hit, serializer)
                };

                if (access != null)
                {
                    data.Translate(access.Language);
                    entry.IsDownloadAllowed = access.HasAnyTokenFor(data.PrimaryDataDownloadAccessTokens);
                }

                entries.Add(entry);
            }

            var entityResult = new EntityResult <T> {
                Items = entries
            };

            if (response?.Aggregations.Any() != null)
            {
                var filteredAggregations = GetfilteredAggregations(response.Aggregations, facetsFilters, out var chosenCreationPeriodAggregation);
                var facette = filteredAggregations.CreateSerializableAggregations();

                ComplementAggregations(facette, chosenCreationPeriodAggregation);
                result.Facets = facette;
            }

            result.Data = entityResult;
        }
 public string SerializeToString(object value)
 {
     return(_elasticsearchSerializer.SerializeToString(value, formatting: SerializationFormatting.None));
 }