Skip to content

to_json returns null for empty map and vector types #399

@totalgee

Description

@totalgee
  • Compiler Used: Xcode 9.2
  • Operating System: macOS 10.12.6
  • Architecture (ARM/x86/32bit/64bit/etc): Intel Core i5 64bit

Expected vs Actual Behavior

If you call to_json([]) (aka to_json(Vector())) or to_json(Map()) you get the string "null", whereas it makes more "semantic" sense to return an empty JSON object "{}" or array "[]", respectively, as that's what you gave it.

This kind-of also relates to the fact that a "minimal" JSON text should be an empty object or array, not a standalone value (including "null")....though some JSON parsers do support free-standing values. Even so, it makes more sense to allow a round-trip to maintain the type (e.g. from_json(to_json([])) == []).

Minimal Example to Reproduce Behavior

// The following should pass (added to new unit test):
assert_equal(from_json(to_json([])), [])
assert_equal(from_json(to_json(Map())), Map())

The above examples currently fail, because to_json([]) returns "null".

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions