IProxy implementation to allow JSON as the input to Duck.Implement<T>(T source)
$ dotnet add package Shadow.Quack.JsonThis package allows the easy population of values directly from a JSON string to a dynamically implemented immutable interface with the help of the base Shadow Quack package.
This means that there is almost no effort in deserialising from JSON to an interface with no need for concrete implementations or additional code.
public interface IJsonProxyTestInterface
{
bool Boolean { get; }
decimal DecimalNumber { get; }
int Number { get; }
IEnumerable<int> Numbers { get; }
IEnumerable<string> Strings { get; }
string Name { get; }
string Type { get; }
}
JsonProxy input = "{\"boolean\":true}";
var result = Duck.Implement<IJsonProxyTestInterface>(input);
var input = "{\"number\":1, \"decimalNumber\":1.6}";
var result = Duck.Implement<IJsonProxyTestInterface>((JsonProxy) input);
var result = Duck.Implement<IJsonProxyTestInterface>((JsonProxy) "{\"numbers\":[1,2,3,4,5]}");
JsonProxy input = "{\"name\":\"value\",\"type\":\"value2\" }";
var result = Duck.Implement<IJsonProxyTestInterface>(input);
This ability has applications within unit tests, to reduce the number of mock data objects, required, and within production code to read in/translate configuration and JSON data without the need for a class implementation.