private static EntityWithValues DeserializeEntity(string value)
        {
            var dictionary = JsonSerializer.DeserializeFromString <Dictionary <string, string> >(value);

            if (dictionary == null)
            {
                return(null);
            }
            var entity = new EntityWithValues();

            foreach (var pair in dictionary)
            {
                if (pair.Key == "id")
                {
                    if (!string.IsNullOrEmpty(pair.Value))
                    {
                        entity.id = int.Parse(pair.Value);
                    }
                }
                else
                {
                    entity.Values.Add(pair.Key, pair.Value);
                }
            }
            return(entity);
        }
 private static string SerializeEntity(EntityWithValues entity)
 {
     var dictionary = entity.Values.ToDictionary(pair => pair.Key, pair => pair.Value);
     if (entity.id > 0) {
         dictionary["id"] = entity.id.ToString(CultureInfo.InvariantCulture);
     }
     return JsonSerializer.SerializeToString(dictionary);
 }
Example #3
0
 private static string SerializeEntity(EntityWithValues entity)
 {
     var dictionary = entity.Values.ToDictionary(pair => pair.Key, pair => pair.Value);
     if (entity.id > 0)
     {
         dictionary["id"] = entity.id.ToString(CultureInfo.InvariantCulture);
     }
     return JsonSerializer.SerializeToString(dictionary);
 }
        public void Can_serialize_Entity()
        {
            var originalEntity = new EntityWithValues {
                id = 5, Values = new Dictionary <string, string> {
                    { "dog", "bark" }, { "cat", "meow" }
                }
            };

            JsonSerializeAndCompare(originalEntity);
        }
 private static EntityWithValues DeserializeEntity(string value)
 {
     var dictionary = JsonSerializer.DeserializeFromString<Dictionary<string, string>>(value);
     if (dictionary == null) return null;
     var entity = new EntityWithValues();
     foreach (var pair in dictionary) {
         if (pair.Key == "id") {
             if (!string.IsNullOrEmpty(pair.Value)) {
                 entity.id = int.Parse(pair.Value);
             }
         } else {
             entity.Values.Add(pair.Key, pair.Value);
         }
     }
     return entity;
 }
 public bool Equals(EntityWithValues other)
 {
     return(ReferenceEquals(this, other) ||
            (this.id == other.id && DictionaryEquality(Values, other.Values)));
 }
 /// <summary>Tests if this EntityWithValues is considered equal to another.</summary>
 /// <param name="other">The entity with values to compare to this object.</param>
 /// <returns>true if the objects are considered equal, false if they are not.</returns>
 public bool Equals(EntityWithValues other)
 {
     return ReferenceEquals(this, other)
            || (this.id == other.id && DictionaryEquality(Values, other.Values));
 }
 public void Can_serialize_Entity()
 {
     var originalEntity = new EntityWithValues { id = 5, Values = new Dictionary<string, string> { { "dog", "bark" }, { "cat", "meow" } } };
     JsonSerializeAndCompare(originalEntity);
 }