1

I have OData model for class X:

<EntityType Name="X" OpenType="true">
  <Key>
    <PropertyRef Name="id" />
  </Key>
  <Property Name="name" Type="Edm.String" />
  <Property Name="logo" Type="Edm.String" />
</EntityType>

When I post new entity for this class, if the field is open type and is Utf8 the property field (Open type IDictionary<string, object>) populate with those UTF characters. If I do the same with field that is not open type (name for example) the data is ok.

For example: If I send request to create new entity X with (This is the json I send within the POST request)

{"name":"ä,ö,ü", "nameOpenType":"ä,ö,ü"} 

it will be serialized into (This is the entity that I got in the OData controller)

{"name":"ä,ö,ü", "nameOpenType":"\u00e4,\u00f6,\u00fc"}

The problem is that "nameOpenType" become unreadable string instead of being the same as "name".
The serialization is done by OData default when I send POST request to the OData controller.
How can I fix this?

guylot
  • 201
  • 2
  • 13
  • The serialization looks like JSON. Both before and after are valid, equivalent JSON documents (though the second one looks like it is of two minds). The serialization is probably working as designed. So, please [edit] your question to give information about how the problem is manifested. When transferred, is it properly identified to the recipient as JSON? When deserialized, is it by a validated JSON library? – Tom Blodget Dec 19 '18 at 22:29

1 Answers1

1

I can reproduce the problem. UTF8 is received OK by the controller. Then when model binding the odata deserializer gets it wrong.

Why oh why do we still have these kind of issues. Someone invent another string encoding, having an identifier built in, like the BOM for UTF8 files.

To fix it and read the intended string:

   static string DecodeEncodedNonAsciiCharacters(string value)
    {
        return Regex.Replace(
            value,
            @"\\u(?<Value>[a-zA-Z0-9]{4})",
            m => { return ((char)int.Parse(m.Groups["Value"].Value,
            NumberStyles.HexNumber)).ToString(); });
    }

Code from here https://stackoverflow.com/a/1615860/5223937

  • But the encoding problem only happens on OData open type property. If the field is exists on the mapping the encoding will be fine, and odata code is responsible for the open type filling. Can I define open type character set? Edit: I tried it with the OData opentype sample and on this demo project I have the same problem if I put open type value like "ä,ö,ü" than it becomes to "\\u00e4,\\u00f6,\\u00fc" – guylot Dec 19 '18 at 16:31
  • Ah, got it. Will rewrite the answear. – Jonas Jakobsson Dec 20 '18 at 15:45
  • Thanks, I was afraid that this solution will be the only way. Kinda hoped for any built in solution from OData but right now this is the only one that work for me – guylot Dec 23 '18 at 15:17