コード例 #1
0
        /// <summary>
        /// Gets the attributes JSON object that represents an entity
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="context"></param>
        public static JsonObject GetAttributes(
            Entity entity,
            SerializationContext context
            )
        {
            JsonObject attributes = DefaultSerializer.ToJson(
                entity,
                entity.GetType(),
                context
                );

            // add "_key" attribute
            attributes["_key"] = DocumentId.Parse(
                attributes["_id"].AsString
                ).Key;

            return(attributes);
        }
コード例 #2
0
        private static JsonValue Serialize(
            object subject,
            Type typeScope,
            SerializationContext context
            )
        {
            Type type = subject.GetType();

            // .NET primitives
            if (type.IsPrimitive)
            {
                return(DotNetPrimitivesSerializer.ToJson(subject, type));
            }

            // string
            if (type == typeof(string))
            {
                return((string)subject);
            }

            // enums
            if (type.IsEnum)
            {
                return(EnumSerializer.ToJson(subject));
            }

            // arrays
            if (type.IsArray)
            {
                // binary data
                if (type == typeof(byte[]))
                {
                    return(BinarySerializer.ToJson(subject, typeScope, context));
                }

                return(ArraySerializer.ToJson(subject, typeScope, context));
            }

            // by what type value to search through ITypeSerializers
            var searchType = type.IsGenericType
                ? type.GetGenericTypeDefinition()
                : type;

            // exact type serializers
            if (exactTypeSerializers.TryGetValue(searchType, out ITypeSerializer serializer))
            {
                return(serializer.ToJson(subject, typeScope, context));
            }

            // assignable type serializers
            foreach (var pair in assignableTypeSerializers)
            {
                if (pair.Key.IsAssignableFrom(searchType))
                {
                    return(pair.Value.ToJson(subject, typeScope, context));
                }
            }

            // unisave serializable
            if (typeof(IUnisaveSerializable).IsAssignableFrom(type))
            {
                return(UnisaveSerializableTypeSerializer.ToJson(
                           subject, typeScope, context
                           ));
            }

            // serializable
            if (typeof(ISerializable).IsAssignableFrom(type))
            {
                return(SerializableTypeSerializer.ToJson(
                           subject, typeScope, context
                           ));
            }

            // validate type scope
            if (!typeScope.IsAssignableFrom(type))
            {
                throw new ArgumentException(
                          $"Given subject is of type {type} that is not assignable " +
                          $"to the given type scope {typeScope}"
                          );
            }

            // other
            return(DefaultSerializer.ToJson(subject, typeScope, context));
        }