1

I faced a problem with wrong sorting JSON keys. I use mongo db and I need to send a creating user form command. vibe-d JSON:

            Json a2 = Json([
                    "createUser": Json(req.form["user"]),
                    "pwd": Json(req.form["password"]),
                    "roles": Json([
                            Json([
                                "role": Json(req.form["access"]),
                                "db": Json("3dstore")
                            ])
                        ])
                    ]);
            logInfo(a2.toString());

Output:

[main(Wbp2) INF] {"roles":[{"role":"readWrite","db":"3dstore"}],"createUser":"111","pwd":"1"}

std.json:

JSONValue a2 = JSONValue([
                    "createUser": JSONValue(req.form["user"]),
                    "pwd": JSONValue(req.form["password"]),
                    "roles": JSONValue([
                            JSONValue([
                                "role": JSONValue(req.form["access"]),
                                "db": JSONValue("3dstore")
                            ])
                        ])
                    ]);
            logInfo(a2.toString());

Output:

[main(vVOX) INF] {"createUser":"111","pwd":"1","roles":[{"db":"3dstore","role":"readWrite"}]}

Therefore I get an error in mongo output: "errmsg" : "no such command: 'roles'"

Any ideas?

Wusiki Jeronii
  • 159
  • 1
  • 8
  • It works with a custom struct but it's an extra code. The question is open. – Wusiki Jeronii May 15 '21 at 18:59
  • Are you sure the sorting is the source of the error ? Dictionaries are (almost) always not sorted by key, and it would seem odd that something accepting a dictionary would be sensitive to the order. Are you passing the value to mongodb as a string ? – Geod24 May 17 '21 at 04:33
  • @Geod24, almost yes. Yes, I thought so. Json object doesn't have any info about it sorting like AA. But Json it is serialized object which d can understand. But runCommand it is command to send pack of bytes to mongo shell. Mongo shell at first it's just socket interface between OS and mongo db. Sockets don't store serialized information. They can accept only bytes or chars. And when you send a command to a shell you send deserialized Json as a string. For example, if you open a terminal and execute same command you've got the same error – Wusiki Jeronii May 17 '21 at 07:50
  • I send command as as Json struct instance. Worth to register an issue on their git? – Wusiki Jeronii May 17 '21 at 07:52
  • If they expect json keys to be sorted, then that is a bug I would say... – DejanLekic May 20 '21 at 12:20

0 Answers0