0

I receive some JSON from a Java third-party system that contains Avro schemas in JSON format. An example looks like this:

{"type":"record", "name":"AvroRecord", "namespace":"Parent.Namespace", "fields": [{"name":"AvroField", "type":"bytes", "default":"\u00FF"}]}

I parse this JSON to do some C# code generation. The result would look like this:

public partial class AvroRecord
{
    [AvroField(Name = "AvroField", Type = "bytes", DefaultValueText = "ÿ")]
    public byte[] AvroField { get; set; }

    public AvroRecord() {  this.AvroField = new byte[] { 255 }; }
}

Eventually, from the C# representation of the schema, I need to infer back the original schema. Once I get that inferred schema, it will be sent over to the original system for comparison. That is why I want to keep the original string value for the default value, since I don't know if:

{"type":"record", "name":"AvroRecord", "namespace":"Parent.Namespace", "fields": [{"name":"AvroField", "type":"bytes", "default":"\u00FF"}]}

and

{"type":"record", "name":"AvroRecord", "namespace":"Parent.Namespace", "fields": [{"name":"AvroField", "type":"bytes", "default":"ÿ"}]}

will result in an exact match or it will have a problem.

I use JSON.NET to convert from the raw schema as a string to something more useful that I can work with:

JToken token = JToken.Parse(schema);

Is there a way in JSON.NET or any other JSON parsing library to control the parsing and copy a value without being parsed? Basically, a way to avoid "\u00FF" becoming "ÿ"

24
  • @JamesThorpe I changed the wording, wasn't very clear. I'd like it "raw" Commented Sep 7, 2017 at 11:41
  • 1
    This is not a JSON "problem". \u00FF is the unicode character code for ÿ. You can probably get the numeric value of ÿ somehow as hex (0x00FF) and "build" @"\u00FF" from there. I'd be surprised if there is no built in functionality for that, but I don't know of any. Commented Sep 7, 2017 at 11:50
  • 1
    It is correct. You can have as many or few leading zeros as you like and it be the same value. At this point, I'm beginning to think this is looking like an XY Problem - you might want to give a wider context around what it is you're actually trying to achieve. Commented Sep 7, 2017 at 12:00
  • 2
    @JavierHolguera - I did notice that. Notice also that ÿ is in double quotes, too. -- "Hello \u00FF World!" is the exact same thing as "Hello ÿ World!". -- in other words: "\u00FF" == "ÿ" results in true. Commented Sep 7, 2017 at 12:02
  • 1
    Json parses and returns values exactly as they were initially. The problem is that you are inspecting input and output differently: input as escape sequence, but output as an unicode string. Use same method to look at content, run debugger and look at schema. Commented Sep 7, 2017 at 12:09

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.