コード例 #1
0
        public void CanSerializeYieldGetterMethods()
        {
            // If this test breaks, this is likely becaused we merged
            // a new version of JSON.Net, which can revert a modification that we made to the code
            // Look at the other changes that happened in this commit (see the git log for that)
            // And at any rate, the full explanation, including the full reasoning is here:
            // http://issues.hibernatingrhinos.com/issue/RavenDB-3931
            //
            // Don't try to fix this issue without reading the details, it is a single line fix, but it
            // takes time to get to the right reason

            var DocumentConventions = new DocumentConventions();
            var jsonSerializer      = DocumentConventions.CreateSerializer();
            var stringWriter        = new StringWriter();

            jsonSerializer.Serialize(stringWriter, new Item());
            var str = stringWriter.GetStringBuilder().ToString();

            jsonSerializer.Deserialize <Item>(new JsonTextReader(new StringReader(str)));
        }
コード例 #2
0
        public static BlittableJsonReaderObject ConvertEntityToBlittable(object entity, DocumentConventions conventions, JsonOperationContext context, DocumentInfo documentInfo = null)
        {
            using (var writer = new BlittableJsonWriter(context, documentInfo))
            {
                var serializer = conventions.CreateSerializer();

                serializer.Serialize(writer, entity);
                writer.FinalizeDocument();
                var reader = writer.CreateReader();
                var type   = entity.GetType();

                var changes = TryRemoveIdentityProperty(reader, type, conventions);
                changes |= TrySimplifyJson(reader);

                if (changes)
                {
                    reader = context.ReadObject(reader, "convert/entityToBlittable");
                }

                return(reader);
            }
        }
コード例 #3
0
ファイル: TypeConverter.cs プロジェクト: haludi/ravendb
        public static object ToBlittableSupportedType(object value, DocumentConventions conventions, JsonOperationContext context)
        {
            if (value == null)
            {
                return(null);
            }

            var type           = value.GetType();
            var underlyingType = Nullable.GetUnderlyingType(type);

            if (underlyingType != null)
            {
                type = underlyingType;
            }

            if (type == typeof(string))
            {
                return(value);
            }

            if (type == typeof(LazyStringValue) || type == typeof(LazyCompressedStringValue))
            {
                return(value);
            }

            if (type == typeof(bool))
            {
                return(value);
            }

            if (type == typeof(int) || type == typeof(long) || type == typeof(double) ||
                type == typeof(decimal) || type == typeof(float) || type == typeof(uint) ||
                type == typeof(ulong) || type == typeof(short) || type == typeof(byte))
            {
                return(value);
            }

            if (type == typeof(LazyNumberValue))
            {
                return(value);
            }

            if (type == typeof(DateTime) || type == typeof(DateTimeOffset) || type == typeof(TimeSpan))
            {
                return(value);
            }

            if (type == typeof(Guid))
            {
                return(((Guid)value).ToString("D"));
            }

            if (type.GetTypeInfo().IsSubclassOf(typeof(Enum)))
            {
                return(value.ToString());
            }

            if (value is byte[] bytes)
            {
                return(Convert.ToBase64String(bytes));
            }

            if (value is IDictionary dictionary)
            {
                var @object = new DynamicJsonValue();
                foreach (var key in dictionary.Keys)
                {
                    @object[key.ToString()] = ToBlittableSupportedType(dictionary[key], conventions, context);
                }

                return(@object);
            }

            if (value is IEnumerable enumerable)
            {
                var items = value is IEnumerable <object> objectEnumerable
                    ? objectEnumerable.Select(x => ToBlittableSupportedType(x, conventions, context))
                    : enumerable.Cast <object>().Select(x => ToBlittableSupportedType(x, conventions, context));

                return(new DynamicJsonArray(items));
            }

            using (var writer = new BlittableJsonWriter(context))
            {
                var serializer = conventions.CreateSerializer();

                writer.WriteStartObject();
                writer.WritePropertyName("Value");

                serializer.Serialize(writer, value);

                writer.WriteEndObject();

                writer.FinalizeDocument();

                var reader = writer.CreateReader();
                return(reader["Value"]);
            }
        }
コード例 #4
0
        public static void WriteIndexQuery(this BlittableJsonTextWriter writer, DocumentConventions conventions, JsonOperationContext context, IndexQuery query)
        {
            writer.WriteStartObject();

            writer.WritePropertyName(nameof(query.Query));
            writer.WriteString(query.Query);
            writer.WriteComma();

            if (query.PageSizeSet && query.PageSize >= 0)
            {
                writer.WritePropertyName(nameof(query.PageSize));
                writer.WriteInteger(query.PageSize);
                writer.WriteComma();
            }

            if (query.WaitForNonStaleResults)
            {
                writer.WritePropertyName(nameof(query.WaitForNonStaleResults));
                writer.WriteBool(query.WaitForNonStaleResults);
                writer.WriteComma();
            }

            if (query.Start > 0)
            {
                writer.WritePropertyName(nameof(query.Start));
                writer.WriteInteger(query.Start);
                writer.WriteComma();
            }

            if (query.WaitForNonStaleResultsTimeout.HasValue)
            {
                writer.WritePropertyName(nameof(query.WaitForNonStaleResultsTimeout));
                writer.WriteString(query.WaitForNonStaleResultsTimeout.Value.ToInvariantString());
                writer.WriteComma();
            }

            if (query.DisableCaching)
            {
                writer.WritePropertyName(nameof(query.DisableCaching));
                writer.WriteBool(query.DisableCaching);
                writer.WriteComma();
            }

#if FEATURE_SHOW_TIMINGS
            if (query.ShowTimings)
            {
                writer.WritePropertyName(nameof(query.ShowTimings));
                writer.WriteBool(query.ShowTimings);
                writer.WriteComma();
            }
#endif

            if (query.SkipDuplicateChecking)
            {
                writer.WritePropertyName(nameof(query.SkipDuplicateChecking));
                writer.WriteBool(query.SkipDuplicateChecking);
                writer.WriteComma();
            }

            writer.WritePropertyName(nameof(query.QueryParameters));
            if (query.QueryParameters != null)
            {
                writer.WriteObject(EntityToBlittable.ConvertEntityToBlittable(query.QueryParameters, conventions, context, conventions.CreateSerializer(), documentInfo: null));
            }
            else
            {
                writer.WriteNull();
            }

            writer.WriteEndObject();
        }
コード例 #5
0
 public static BlittableJsonReaderObject ConvertEntityToBlittable(
     object entity,
     DocumentConventions conventions,
     JsonOperationContext context,
     JsonSerializer serializer = null,
     DocumentInfo documentInfo = null)
 {
     using (var writer = new BlittableJsonWriter(context, documentInfo))
         return(ConvertEntityToBlittableInternal(entity, conventions, context, serializer ?? conventions.CreateSerializer(), writer));
 }
コード例 #6
0
            public override HttpRequestMessage CreateRequest(JsonOperationContext ctx, ServerNode node, out string url)
            {
                url = $"{node.Url}/databases/{node.Database}/cmpxchg?key={_key}&index={_index}";
                var djv = new DynamicJsonValue
                {
                    ["Object"] = EntityToBlittable.ConvertToBlittableIfNeeded(_value, _conventions, ctx, _conventions.CreateSerializer(), documentInfo: null, removeIdentityProperty: false)
                };
                var blittable = ctx.ReadObject(djv, _key);

                var request = new HttpRequestMessage
                {
                    Method  = HttpMethods.Put,
                    Content = new BlittableJsonContent(stream =>
                    {
                        ctx.Write(stream, blittable);
                    })
                };

                return(request);
            }
コード例 #7
0
ファイル: TypeConverter.cs プロジェクト: otgoo0603/ravendb
        public static object ToBlittableSupportedType(object value, DocumentConventions conventions, JsonOperationContext context)
        {
            if (value == null)
            {
                return(null);
            }

            var type           = value.GetType();
            var underlyingType = Nullable.GetUnderlyingType(type);

            if (underlyingType != null)
            {
                type = underlyingType;
            }

            if (type == typeof(string))
            {
                return(value);
            }

            if (type == typeof(LazyStringValue) || type == typeof(LazyCompressedStringValue))
            {
                return(value);
            }

            if (type == typeof(bool))
            {
                return(value);
            }

            if (type == typeof(int) || type == typeof(long) || type == typeof(double) || type == typeof(decimal) || type == typeof(float))
            {
                return(value);
            }

            if (type == typeof(LazyNumberValue))
            {
                return(value);
            }

            if (type == typeof(DateTime) || type == typeof(DateTimeOffset) || type == typeof(TimeSpan))
            {
                return(value);
            }

            if (type == typeof(Guid))
            {
                return(((Guid)value).ToString("D"));
            }

            if (type == typeof(Enum))
            {
                return(value.ToString());
            }

            var dictionary = value as IDictionary;

            if (dictionary != null)
            {
                var @object = new DynamicJsonValue();
                foreach (var key in dictionary.Keys)
                {
                    @object[key.ToString()] = ToBlittableSupportedType(dictionary[key], conventions, context);
                }

                return(@object);
            }

            var enumerable = value as IEnumerable;

            if (enumerable != null)
            {
                var objectEnumerable = value as IEnumerable <object>;
                var items            = objectEnumerable != null
                    ? objectEnumerable.Select(x => ToBlittableSupportedType(x, conventions, context))
                    : enumerable.Cast <object>().Select(x => ToBlittableSupportedType(x, conventions, context));

                return(new DynamicJsonArray(items));
            }

            using (var writer = new BlittableJsonWriter(context))
            {
                var serializer = conventions.CreateSerializer();

                serializer.Serialize(writer, value);
                writer.FinalizeDocument();

                return(writer.CreateReader());
            }
        }