c# - Deserializing Settings Stored As JSON In CRM Web Resource -
i've added configuration data json in web resource in crm. when attempt deserialize in c# plugin have first decode base 64 string, , have encode utf bytes. i've done before outside of crm , haven't had issue, crm threw me loop when json wasn't deserializing due argument exception "invalid json primitive: ."
i figured out crm includes utf preamble , isn't valid json have remove it. below current solution, have thought there standard way encode (or decode) bytes check see preamble was, , correctly apply correct encoding , return result without preamble.
private static settings deserialize(webresource value) { if (value == null) { throw new argumentnullexception("value"); } // default, content stored in base64string utf preamble. var content = encoding.utf8.getstring(convert.frombase64string(value.content)); var preamble = encoding.utf8.getstring(encoding.utf8.getpreamble()); if (content.startswith(preamble)) { content = content.remove(0, preamble.length); } return new system.web.script.serialization.javascriptserializer().deserialize<settings>(content); }
surely there more standard way of doing this?
i dived encoding class structure , noticed takes boolean encodershouldemitutf8identifier
value determine if preamble should emitted. unfortunately, thing if set false, getpreamble()
return empty value. means have manually check , remove preamble if exists.
Comments
Post a Comment