{"comment":{"url":"https://api.github.com/repos/bretthysuik/UnitsNet/comments/12743141","html_url":"https://github.com/bretthysuik/UnitsNet/commit/87f4df893cd4dfc747de944568c975413913bf2d#commitcomment-12743141","id":12743141,"user":{"login":"bretthysuik","id":2490331,"avatar_url":"https://avatars.githubusercontent.com/u/2490331?v=3","gravatar_id":"","url":"https://api.github.com/users/bretthysuik","html_url":"https://github.com/bretthysuik","followers_url":"https://api.github.com/users/bretthysuik/followers","following_url":"https://api.github.com/users/bretthysuik/following{/other_user}","gists_url":"https://api.github.com/users/bretthysuik/gists{/gist_id}","starred_url":"https://api.github.com/users/bretthysuik/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bretthysuik/subscriptions","organizations_url":"https://api.github.com/users/bretthysuik/orgs","repos_url":"https://api.github.com/users/bretthysuik/repos","events_url":"https://api.github.com/users/bretthysuik/events{/privacy}","received_events_url":"https://api.github.com/users/bretthysuik/received_events","type":"User","site_admin":false},"position":95,"line":95,"path":"UnitsNet.Serialization.JsonNet.Tests/UnitsNetJsonConverterBaseTests.cs","commit_id":"87f4df893cd4dfc747de944568c975413913bf2d","created_at":"2015-08-17T16:53:05Z","updated_at":"2015-08-17T16:53:05Z","body":"OK, after doing some more research, I've come across the following hacky solution which would allow us to dynamically parse the unit from the unit enum string using reflection:\r\n\r\n```C#\r\npublic override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)\r\n{\r\n    double value = 0;\r\n    string unitEnumTypeAndValue = null;\r\n    while (reader.Read())\r\n    {\r\n        switch (reader.TokenType)\r\n        {\r\n            case JsonToken.Float:\r\n            case JsonToken.Integer:\r\n                value = Convert.ToDouble(reader.Value);\r\n                break;\r\n            case JsonToken.String:\r\n                unitEnumTypeAndValue = reader.Value.ToString();\r\n                break;\r\n        }\r\n    }\r\n    if (unitEnumTypeAndValue == null)\r\n        return default(TUnit);\r\n\r\n    string unitEnumType = unitEnumTypeAndValue.Split('.')[0];\r\n    string unitEnumValue = unitEnumTypeAndValue.Split('.')[1];\r\n\r\n    // Ex: UnitsNet.Units.MassUnit,UnitsNet -- see http://stackoverflow.com/a/6465096/1256096 for details\r\n    Type reflectedUnitEnumType = Type.GetType(\"UnitsNet.Units.\" + unitEnumType + \",UnitsNet\");\r\n    object unit = Enum.Parse(reflectedUnitEnumType, unitEnumValue);\r\n\r\n    MethodInfo fromMethod = typeof(TUnit).GetMethod(\"From\");\r\n\r\n    // Ex: Mass.From(55, MassUnit.Grams)\r\n    return fromMethod.Invoke(null, BindingFlags.Static, null, new[] { value, unit }, null);\r\n}\r\n```\r\n\r\nThe major downside here is that the namespace (`UnitsNet.Units.*`) is hardcoded and the main assembly name is hardcoded (`UnitsNet`)."}}