ovsdb_datum_to_json(const struct ovsdb_datum *datum,
const struct ovsdb_type *type)
{
- if (datum->n == 1 && !ovsdb_type_is_map(type)) {
- return ovsdb_atom_to_json(&datum->keys[0], type->key.type);
- } else if (type->value.type == OVSDB_TYPE_VOID) {
+ if (ovsdb_type_is_map(type)) {
struct json **elems;
size_t i;
elems = xmalloc(datum->n * sizeof *elems);
for (i = 0; i < datum->n; i++) {
- elems[i] = ovsdb_atom_to_json(&datum->keys[i], type->key.type);
+ elems[i] = json_array_create_2(
+ ovsdb_atom_to_json(&datum->keys[i], type->key.type),
+ ovsdb_atom_to_json(&datum->values[i], type->value.type));
}
- return wrap_json("set", json_array_create(elems, datum->n));
+ return wrap_json("map", json_array_create(elems, datum->n));
+ } else if (datum->n == 1) {
+ return ovsdb_atom_to_json(&datum->keys[0], type->key.type);
} else {
struct json **elems;
size_t i;
elems = xmalloc(datum->n * sizeof *elems);
for (i = 0; i < datum->n; i++) {
- elems[i] = json_array_create_2(
- ovsdb_atom_to_json(&datum->keys[i], type->key.type),
- ovsdb_atom_to_json(&datum->values[i], type->value.type));
+ elems[i] = ovsdb_atom_to_json(&datum->keys[i], type->key.type);
}
- return wrap_json("map", json_array_create(elems, datum->n));
+ return wrap_json("set", json_array_create(elems, datum->n));
}
}
return Datum(type_, {keyAtom: None})
def to_json(self):
- if len(self.values) == 1 and not self.type.is_map():
+ if self.type.is_map():
+ return ["map", [[k.to_json(), v.to_json()]
+ for k, v in sorted(self.values.items())]]
+ elif len(self.values) == 1:
key = self.values.keys()[0]
return key.to_json()
- elif not self.type.is_map():
- return ["set", [k.to_json() for k in sorted(self.values.keys())]]
else:
- return ["map", [[k.to_json(), v.to_json()]
- for k, v in sorted(self.values.items())]]
+ return ["set", [k.to_json() for k in sorted(self.values.keys())]]
def to_string(self):
head = tail = None