コード例 #1
0
        /// <summary>
        /// deserialize a json string to an owner instance
        /// </summary>
        /// <param name="obj">json string</param>
        /// <returns>owner instance</returns>
        public static Owner DeserializeOwner(string obj)
        {
            Owner.Builder builder = new Owner.Builder();
            Dictionary <string, object> attributes = new Dictionary <string, object>();

            var rawOwner = JsonConvert.DeserializeObject <Dictionary <string, object> >(obj,
                                                                                        new JsonSerializerSettings
            {
                NullValueHandling    = NullValueHandling.Ignore,
                DateFormatString     = EventSerializer.DatetimeFormat,
                DateTimeZoneHandling = DateTimeZoneHandling.Utc
            });

            foreach (KeyValuePair <string, object> token in rawOwner)
            {
                switch (token.Key)
                {
                case UsernameProperty:
                {
                    if (!(token.Value is string))
                    {
                        throw new InvalidOperationException("Field 'username' does not match TYPE 'TEXT'");
                    }
                    builder.Username = token.Value as string;
                    break;
                }

                case RegistrationDateProperty:
                {
                    if (token.Value == null || !(token.Value is DateTime))
                    {
                        throw new InvalidOperationException("Field 'x_registration_date' does not match TYPE 'DATETIME'");
                    }
                    builder.RegistrationDateTime = new DateTimeOffset((DateTime)token.Value);
                    break;
                }

                case PasswordProperty:
                {
                    if (!(token.Value is string))
                    {
                        throw new InvalidOperationException("Field 'x_password' does not match TYPE 'TEXT'");
                    }
                    builder.Password = token.Value as string;
                    break;
                }

                default:
                {
                    if (token.Value is JArray)
                    {
                        attributes.Add(token.Key, (token.Value as JArray).ToObject <string[]>());
                    }
                    else
                    {
                        attributes.Add(token.Key, token.Value);
                    }
                    break;
                }
                }
            }
            builder.Attributes = attributes;

            return(builder.Build());
        }