Skip to content

Is it possible to handle custom units in json serialization? #934

@giantmustache

Description

@giantmustache

Is your feature request related to a problem? Please describe.
I am trying to work out a new unit. Since it is rather custom for our domain, we thought it would be best to work this out via the wiki page: https://github.com/angularsen/UnitsNet/wiki/Extending-with-Custom-Units.

It seems that the json converters cannot handle this new quantity type, since there is a direct check on the assembly to see if it can find the correct unit:

var unitEnumTypeAssemblyQualifiedName = "UnitsNet.Units." + unitEnumTypeName + ",UnitsNet";
// -- see http://stackoverflow.com/a/6465096/1256096 for details
var unitEnumType = Type.GetType(unitEnumTypeAssemblyQualifiedName);
if (unitEnumType == null)
{
var ex = new UnitsNetException("Unable to find enum type.");
ex.Data["type"] = unitEnumTypeAssemblyQualifiedName;
throw ex;
}

Describe the solution you'd like
It would be nice if there would be a fallback mechanism that can also handle custom types. Just like the conversion functions and abbreviations, some kind of mapping mechanism could help to find more types than just the ones in the UnitsNet assembly.

Describe alternatives you've considered
As an alternative, I can write my own custom JsonConverter and use a decorator pattern to wrap the UnitsNetIQuantityJsonConverter into my own JsonConverter and ensure that GetUnit comes with correct values. These functions are not virtual in UnitsNetBaseJsonConverter, so I will need to make my own implementation and cannot make use of the base class functions.

Additional context
It would be much appreciated if the custom units could also be used inside the json serialization context, since this is essential in our software package. I think it will be a good addition to the extensibility of this package.

Greetings,
Marnix

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions