public static AttributeCollection FromJsonElement(JsonElement json)
        {
            var item = new AttributeCollection();

            foreach (var property in json.EnumerateObject())
            {
                item.Add(property.Name, DbValue.FromJsonElement(property.Value));
            }

            return(item);
        }
Example #2
0
        // { "hitCount":{"N":"225"}, "date":{"S":"2011-05-31T00:00:00Z"}, "siteId":{"N":"221051"} }

        public static AttributeCollection FromJson(JsonObject json)
        {
            var item = new AttributeCollection(json.Keys.Count);

            foreach (var property in json)
            {
                item.Add(property.Key, DbValue.FromJson((JsonObject)property.Value));
            }

            return(item);
        }
        internal static AttributeCollection FromObject(object instance, DatasetInfo schema)
        {
            #region Preconditions

            if (instance == null)
            {
                throw new ArgumentNullException(nameof(instance));
            }

            #endregion

            var attributes = new AttributeCollection();

            foreach (var member in schema.Members)
            {
                var value = member.GetValue(instance);

                if (value == null)
                {
                    continue;
                }

                var typeInfo = TypeDetails.Get(member.Type);

                IDbValueConverter converter;

                if (DbValueConverterFactory.TryGet(member.Type, out converter))
                {
                    var dbValue = converter.FromObject(value, member);

                    if (dbValue.Kind == DbValueType.Unknown)
                    {
                        continue;
                    }

                    attributes.Add(member.Name, dbValue);
                }
                else if (member.Type.IsArray)
                {
                    var a = (Array)value;

                    var list = new List <DbValue>(a.Length);

                    foreach (var item in a)
                    {
                        list.Add(new DbValue(FromObject(item)));
                    }

                    attributes.Add(member.Name, new DbValue(list));
                }
                else if (typeInfo.IsList && typeInfo.ElementType != null)
                {
                    var a = (IList)value;

                    var list = new List <DbValue>(a.Count);

                    IDbValueConverter c;

                    if (DbValueConverterFactory.TryGet(typeInfo.ElementType, out c))
                    {
                        foreach (var item in a)
                        {
                            list.Add(c.FromObject(item, member));
                        }
                    }
                    else
                    {
                        foreach (var item in a)
                        {
                            list.Add(new DbValue(FromObject(item)));
                        }
                    }

                    attributes.Add(member.Name, new DbValue(list));
                }
                else
                {
                    try
                    {
                        attributes.Add(member.Name, new DbValue(FromObject(value)));
                    }
                    catch
                    {
                        throw new Exception(value.GetType().Name + "/" + typeInfo.IsList);
                    }
                }
            }

            return(attributes);
        }