Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/ServiceStack.Text/PlatformExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,11 @@ public void SetValue(object instance, object value)
if (SetValueFn == null)
return;

if (value is IReadOnlyDictionary<string, object> dictionary)
{
value = dictionary.FromObjectDictionary(Type);
}

if (!Type.IsInstanceOfType(value))
{
lock (this)
Expand Down
16 changes: 10 additions & 6 deletions src/ServiceStack.Text/TranslateListWithElements.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,8 @@ public static object TryTranslateCollections(Type fromPropertyType, Type toPrope
if (fromElType == null || toElType == null)
return null;

if (fromElType == typeof(object) || toElType.IsAssignableFrom(fromElType))
return TranslateToGenericICollectionCache(fromValue, toPropertyType, toElType);

return null;
return TranslateToGenericICollectionCache(fromValue, toPropertyType, toElType);
}

}

public class ConvertibleTypeKey
Expand Down Expand Up @@ -199,7 +195,15 @@ public static ICollection<T> TranslateToGenericICollection(
var to = (ICollection<T>)CreateInstance(toInstanceOfType);
foreach (var item in fromList)
{
to.Add((T)item);
if (item is IReadOnlyDictionary<string, object> dictionary)
{
var convertedItem = dictionary.FromObjectDictionary<T>();
to.Add(convertedItem);
}
else
{
to.Add((T)item);
}
}
return to;
}
Expand Down
140 changes: 140 additions & 0 deletions tests/ServiceStack.Text.Tests/AutoMappingObjectDictionaryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,146 @@ public void Can_convert_Dictionary_FromObjectDictionary()
Assert.That(to == dict);
}

[Test]
public void Can_convert_inner_dictionary()
{
var map = new Dictionary<string, object>
{
{ "FirstName", "Foo" },
{ "LastName", "Bar" },
{ "Car", new Dictionary<string, object>
{
{ "Name", "Tesla" },
{ "Age", 2 }
}}
};

var user = map.FromObjectDictionary<User>();

Assert.That(user.FirstName, Is.EqualTo("Foo"));
Assert.That(user.LastName, Is.EqualTo("Bar"));
Assert.That(user.Car, Is.Not.Null);
Assert.That(user.Car.Name, Is.EqualTo("Tesla"));
Assert.That(user.Car.Age, Is.EqualTo(2));
}

[Test]
public void Can_convert_inner_collection_of_dictionaries()
{
var map = new Dictionary<string, object>
{
{ "Name", "Tesla" },
{ "Age", "2" },
{ "Specs", new List<Dictionary<string, object>>
{
new Dictionary<string, object>
{
{"Item", "Model"},
{"Value", "S"}
},
new Dictionary<string, object>
{
{"Item", "Engine"},
{"Value", "Electric"}
},
new Dictionary<string, object>
{
{"Item", "Color"},
{"Value", "Red"}
},
new Dictionary<string, object>
{
{"Item", "PowerKW"},
{"Value", 285}
},
new Dictionary<string, object>
{
{"Item", "TorqueNm"},
{"Value", 430}
},
}}
};

var carWithSpecs = map.FromObjectDictionary<CarWithSpecs>();

Assert.That(carWithSpecs.Name, Is.EqualTo("Tesla"));
Assert.That(carWithSpecs.Age, Is.EqualTo(2));
Assert.That(carWithSpecs.Specs.Count, Is.EqualTo(5));
var model = carWithSpecs.Specs.SingleOrDefault(s => s.Item == "Model");
Assert.That(model, Is.Not.Null);
Assert.That(model.Value, Is.EqualTo("S"));
var engine = carWithSpecs.Specs.SingleOrDefault(s => s.Item == "Engine");
Assert.That(engine, Is.Not.Null);
Assert.That(engine.Value, Is.EqualTo("Electric"));
var color = carWithSpecs.Specs.SingleOrDefault(s => s.Item == "Color");
Assert.That(color, Is.Not.Null);
Assert.That(color.Value, Is.EqualTo("Red"));
var power = carWithSpecs.Specs.SingleOrDefault(s => s.Item == "PowerKW");
Assert.That(power, Is.Not.Null);
Assert.That(power.Value, Is.EqualTo("285"));
var torque = carWithSpecs.Specs.SingleOrDefault(s => s.Item == "TorqueNm");
Assert.That(torque, Is.Not.Null);
Assert.That(torque.Value, Is.EqualTo("430"));
}

[Test]
public void Can_convert_inner_array_of_dictionaries()
{
var map = new Dictionary<string, object>
{
{ "Name", "Tesla" },
{ "Age", "2" },
{ "Specs", new[]
{
new Dictionary<string, object>
{
{"Item", "Model"},
{"Value", "S"}
},
new Dictionary<string, object>
{
{"Item", "Engine"},
{"Value", "Electric"}
},
new Dictionary<string, object>
{
{"Item", "Color"},
{"Value", "Red"}
},
new Dictionary<string, object>
{
{"Item", "PowerKW"},
{"Value", 285}
},
new Dictionary<string, object>
{
{"Item", "TorqueNm"},
{"Value", 430}
},
}}
};

var carWithSpecs = map.FromObjectDictionary<CarWithSpecs>();

Assert.That(carWithSpecs.Name, Is.EqualTo("Tesla"));
Assert.That(carWithSpecs.Age, Is.EqualTo(2));
Assert.That(carWithSpecs.Specs.Count, Is.EqualTo(5));
var model = carWithSpecs.Specs.SingleOrDefault(s => s.Item == "Model");
Assert.That(model, Is.Not.Null);
Assert.That(model.Value, Is.EqualTo("S"));
var engine = carWithSpecs.Specs.SingleOrDefault(s => s.Item == "Engine");
Assert.That(engine, Is.Not.Null);
Assert.That(engine.Value, Is.EqualTo("Electric"));
var color = carWithSpecs.Specs.SingleOrDefault(s => s.Item == "Color");
Assert.That(color, Is.Not.Null);
Assert.That(color.Value, Is.EqualTo("Red"));
var power = carWithSpecs.Specs.SingleOrDefault(s => s.Item == "PowerKW");
Assert.That(power, Is.Not.Null);
Assert.That(power.Value, Is.EqualTo("285"));
var torque = carWithSpecs.Specs.SingleOrDefault(s => s.Item == "TorqueNm");
Assert.That(torque, Is.Not.Null);
Assert.That(torque.Value, Is.EqualTo("430"));
}
}


Expand Down
11 changes: 11 additions & 0 deletions tests/ServiceStack.Text.Tests/AutoMappingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,17 @@ public class SubCar : Car
public string Custom { get; set; }
}

public class CarWithSpecs : Car
{
public List<CarSpec> Specs { get; set; }
}

public class CarSpec
{
public string Item { get; set; }
public string Value { get; set; }
}

public class UserDto
{
public string FirstName { get; set; }
Expand Down