Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

map to json #18

Closed
awalterschulze opened this issue Mar 12, 2015 · 10 comments
Closed

map to json #18

awalterschulze opened this issue Mar 12, 2015 · 10 comments

Comments

@awalterschulze
Copy link

Add this function to all_test.go

func TestMapFieldJsonMarshal(t *testing.T) {
    m := &MessageWithMap{
        NameMapping: map[int32]string{
            1: "Rob",
            4: "Ian",
            8: "Dave",
        },
    }
    _, err := json.Marshal(m)
    if err != nil {
        t.Fatal(err)
    }
}

It will fail with
json: unsupported type: map[int32]string

Is this work just not done yet or am I understanding something incorrectly or is this a bug?
I thought that json is the new text standard for proto3 and also that proto3 supports maps?
I would guess that go needs to support marshaling proto3 structures to json or am I interpreting this in the wrong way?

@logrusorgru
Copy link

Because

{
        8: "some"
}

is not valid json. But

{
    "8": "some"
}

is valid. Only string can be key. Use map[string]Type or []Type.

@awalterschulze
Copy link
Author

proto3 allows int32 keys in maps and proto3 says json is its standard text format.

@tv42
Copy link

tv42 commented Mar 12, 2015

@awalterschulze There's no such thing as JSON object with a non-string key. http://json.org/

@awalterschulze
Copy link
Author

Yes I understand.

Let me add some context.

You can have:
"The following are the main new features in language version 3:
...
vii. A well-defined encoding in JSON as an alternative to binary proto encoding."
https://github.com/google/protobuf/releases/tag/v3.0.0-alpha-2

And allow maps with keys int32
"map<int32, string> name_mapping = 14;"
3ea3e05

When json does not support non-string keys
http://json.org/

And not have a plan.

@dsymonds
Copy link
Contributor

I haven't seen a spec for how proto maps are meant to appear in JSON. That's probably something to file as an issue against google/protobuf.

FWIW, I don't think encoding/json is going to work with proto3 for matching the proto3 JSON spec. The encoding/json package has a fairly rigid idea of how things are meant to be rendered. We'll see, but I suspect an auxiliary jsonpb package (which will be able to have all the relevant tuning knobs) will end up as the best approach.

@dsymonds
Copy link
Contributor

To be clear, JSON is not the new "text format" for proto3, but rather it is a new canonical interchange format.

@awalterschulze
Copy link
Author

Ok so if I understand correctly:

  • there will now be four canonical formats, json, prototext, compacttext and proto;
  • golang/protobuf, but first google/protobuf will be working on a plan for map[]string to json.

@dsymonds
Copy link
Contributor

If by "prototext" and "compacttext" you mean the output of proto.MarshalText and proto.CompactText, then they are the same format, just with differing whitespace (ignored by parsers) for layout. But that format is not intended as an interchange format.

So there's the binary wire format, which has been around forever, and now there will be JSON too.

Your second point is accurate enough. There will be a plan for the JSON output of map fields that will be consistent across all proto language implementations; I expect to wait until that is defined before modifying Go's protobuf package to support it.

@awalterschulze
Copy link
Author

Excellent, that is what I want to hear :)

On 13 March 2015 at 12:23, David Symonds notifications@github.com wrote:

If by "prototext" and "compacttext" you mean the output of
proto.MarshalText and proto.CompactText, then they are the same format,
just with differing whitespace (ignored by parsers) for layout. But that
format is not intended as an interchange format.

So there's the binary wire format, which has been around forever, and now
there will be JSON too.

Your second point is accurate enough. There will be a plan for the JSON
output of map fields that will be consistent across all proto language
implementations; I expect to wait until that is defined before modifying
Go's protobuf package to support it.


Reply to this email directly or view it on GitHub
#18 (comment).

@dsymonds
Copy link
Contributor

dsymonds commented Jul 1, 2015

The newly added jsonpb package (see 67cbcad) supports the official JSON output format. It includes map output.

@dsymonds dsymonds closed this as completed Jul 1, 2015
@golang golang locked as resolved and limited conversation to collaborators Jun 25, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants