コード例 #1
0
        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);
        }
コード例 #2
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);
 }
コード例 #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);
 }
コード例 #4
0
        public void Can_serialize_Entity()
        {
            var originalEntity = new EntityWithValues {
                id = 5, Values = new Dictionary <string, string> {
                    { "dog", "bark" }, { "cat", "meow" }
                }
            };

            JsonSerializeAndCompare(originalEntity);
        }
コード例 #5
0
 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;
 }
コード例 #6
0
 public bool Equals(EntityWithValues other)
 {
     return(ReferenceEquals(this, other) ||
            (this.id == other.id && DictionaryEquality(Values, other.Values)));
 }
コード例 #7
0
 /// <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));
 }
コード例 #8
0
 public void Can_serialize_Entity()
 {
     var originalEntity = new EntityWithValues { id = 5, Values = new Dictionary<string, string> { { "dog", "bark" }, { "cat", "meow" } } };
     JsonSerializeAndCompare(originalEntity);
 }