How to convert an orddict payload to json

I have this python Marshmallow schema that is used
to create an OrderedDict of our request data.

class RequestSchema(Schema):
    request_id = fields.Str(data_key="requestId")
    request_timestamp = fields.Str(data_key="requestTimeStamp")
    channel = fields.Str()
    operation = fields.Str()
    request_param = fields.Nested(ListValueSchema, data_key="requestParam")

    class Meta:
        ordered = True

We then convert it to json using RequestSchema.dump()
before sending it as the body in a http post request.

I am rewriting this code in elixir and I stumbled upon this erlang library called orddict
that I can use to convert my payload to an ordered dict in elixir.

My challenge is in the final step of converting the output of orddict to json using Jason.encode

As soon as you convert anything to JSON the order of the keys in an object is not guaranteed. Therefore I’m not sure your objective can be achieved if JSON is the intermediate representation.

Iirc jason added API for ordered objects in its latest version.

1 Like

RFC 7159 says:

An object is an unordered collection of zero or more name/value
pairs, where a name is a string and a value is a string, number,
boolean, null, object, or array

So even if Jason supports it, there isn’t a guarantee the corresponding party would conform to that?

Yeah. Afaik the biggest reason for handling json objects in an ordered fashion is preventing unnecessary differences between input and output when decoding and encoding from and to json is involved. Order shouldn‘t affect the represented data, but it will affect the representation itself.

1 Like

In order to force the order of key-value pairs, I’m afraid you have to use this ugly format:

[
  [k1, v1],
  [k2, v2],
  ...
]

By the way, I used it recently, and it works, though not pretty.

1 Like

Either what @Aetherus said, or, be even more paranoid as my former colleagues from ~19 months ago were. They did this:

{
  "0" => ["key_0", "value_0"],
  "1" => ["key_1", "value_1"]
}

But I like @Aetherus’ idea better.

2 Likes

Or, we can just ditch JSON and embrace msgpack. You can define your own data types in it.

2 Likes

I agree but that comes with friction that many teams aren’t okay with. I personally would go for FlatBuffers first, MessagePack second.

3 Likes

I gave this argument to the API owners and they
said that it is no longer a requirement to have the params ordered
and that it was a case of outdated documentation. :sweat_smile:

Thanks everyone.

4 Likes