Protobuf: jsonpb Unmarshalling support for Struct and Value wkt

Created on 13 Apr 2016  路  14Comments  路  Source: golang/protobuf

Do we have support in jsonpb for unmarshalling Struct and Value wkt? If not, is there any plan to add any?

bug

Most helpful comment

We get the correct json in the other direction (with the indentation and ordering as below):

{
  "actor": {
    "location": {
          "displayName": "Denver, CO",
          "objectType": "place"
        }
  }
}

The code to populate the proto looks a bit ugly, but here it is:

    a := test.Activity{
        Actor: &test.Person{
            Location: &stpb.Value{
                Kind: &stpb.Value_StructValue{
                    StructValue: &stpb.Struct{
                        Fields: map[string]*stpb.Value{
                            "objectType":  &stpb.Value{Kind: &stpb.Value_StringValue{"place"}},
                            "displayName": &stpb.Value{Kind: &stpb.Value_StringValue{"Denver, CO"}},
                        },
                    },
                },
            },
        },
    }
    result, _ := (&jsonpb.Marshaler{Indent: "  ", OrigName: true}).MarshalToString(&a)
    fmt.Println(result)

All 14 comments

Yes, Struct and Value are supported by jsonpb. What are you missing?

For some reason, I'm seeing error messages of the form, 'unknown field "xxx" in structpb.Struct/Value',
and the fields in the protobufs are empty.

Also, I cannot find a unit test that is unmarshalling to be sure I'm doing things correctly.

The spec is at https://developers.google.com/protocol-buffers/docs/proto3#json, mentioned in the jsonpb package docs. You could also just marshal a populated structpb.Struct to see what the format is meant to look like.

Feel free to open a new issue if you find a bug.

Hey David, I'm looking to unmarshal from json and not marshal. There are test cases for marshaling structs, but none for unmarshalling.

You indicated you didn't know whether you were doing things right, which I took to mean you weren't sure what a JSON format Struct is meant to look like. I suggested marshaling from jsonpb to see what it looks like.

If you have specific input data that isn't parsed, show it. I can't help you with anything else. The code in jsonpb works to the best of my knowledge.

Hey David,

Here's a somewhat minimal example:

The proto looks like

syntax = "proto3";

package test;

import "google/protobuf/struct.proto";

message Activity {
  Person actor = 3;
}

message Person {
  google.protobuf.Value location = 9;
}

The json looks like:

{
    "actor": {
        "location": {
            "objectType": "place",
            "displayName": "Denver, CO"
        }
    }
}

The unmarshalled proto comes out to

actor: <
  location: <
  >
>

with the error message unknown field "objectType" in structpb.Value

Yes, and what happens in the other direction? i.e. start with the proto that you expect, and see what jsonpb produces.

We get the correct json in the other direction (with the indentation and ordering as below):

{
  "actor": {
    "location": {
          "displayName": "Denver, CO",
          "objectType": "place"
        }
  }
}

The code to populate the proto looks a bit ugly, but here it is:

    a := test.Activity{
        Actor: &test.Person{
            Location: &stpb.Value{
                Kind: &stpb.Value_StructValue{
                    StructValue: &stpb.Struct{
                        Fields: map[string]*stpb.Value{
                            "objectType":  &stpb.Value{Kind: &stpb.Value_StringValue{"place"}},
                            "displayName": &stpb.Value{Kind: &stpb.Value_StringValue{"Denver, CO"}},
                        },
                    },
                },
            },
        },
    }
    result, _ := (&jsonpb.Marshaler{Indent: "  ", OrigName: true}).MarshalToString(&a)
    fmt.Println(result)

Okay, that's more useful. Thanks.

I'll look into this later.

Any update on this? this make grp gateway useless for us as we use several Struct in our message

/cc @jba

/cc @cybrcodr @dsnet

Closing this now. Feel free to file another issue if there are problems with the implementation.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

tamird picture tamird  路  3Comments

hxzqlh picture hxzqlh  路  5Comments

seungryulchoisc picture seungryulchoisc  路  3Comments

dsnet picture dsnet  路  3Comments

occho picture occho  路  5Comments