-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
jsonpb: Attempting to unmarshal escaped JSON into a protobuf throws an error #605
Comments
This is a bug in Line 845 in 927b659
strconv.Unquote is the wrong function to use as it unquotes a string according to string literals in the Go language specification, and not according to string literals for JSON strings, which have a different grammar.
|
@lukeic , mind posting the sample JSON string that produces this bug. I just want to make sure I got a correct test case. Thanks. |
Just sent out a PR. I think I found a reproducible testcase which I've also added to the tests. I'd still like to see a sample JSON to make sure I'm not missing out on other cases. |
Hey @cybrcodr, awesome to see that you've fixed this. Here's a sample of the JSON I'm using: {
"meta": {
"results": 1489442,
"returned": 5,
"errmsg": "",
"status": 1
},
"wines": [
{
"name": "Conway Deep Sea Chardonnay la Costa Wine Co",
"code": "conway-deep-sea-chardonnay-la-costa-wine-co-2008-1",
"region": "USA > California > Central Coast",
"winery": "Conway Family Wines",
"winery_id": "conway-family-wines",
"varietal": "Chardonnay",
"price": "21.99",
"vintage": "2008",
"type": "White Wine",
"link": "http:\/\/www.snooth.com\/wine\/conway-deep-sea-chardonnay-la-costa-wine-co-2008-1\/",
"tags": "",
"image": "https:\/\/ei.isnooth.com\/multimedia\/0\/2\/8\/image_787698_square.jpeg",
"snoothrank": 3,
"available": 0,
"num_merchants": 0,
"num_reviews": 10
},
{
"name": "Olmaia Cabernet di Toscana",
"code": "olmaia-cabernet-di-toscana",
"region": "Italy > Tuscany > Toscana Igt",
"winery": "Col D Orcia",
"winery_id": "col-d-orcia",
"varietal": "Cabernet Sauvignon",
"price": "0.00",
"vintage": "",
"type": "Red Wine",
"link": "http:\/\/www.snooth.com\/wine\/olmaia-cabernet-di-toscana\/",
"tags": "",
"image": "https:\/\/ei.isnooth.com\/multimedia\/d\/e\/e\/image_790198_square.jpeg",
"snoothrank": 3.5,
"available": 0,
"num_merchants": 0,
"num_reviews": 25
},
{
"name": "Dominio Dostares Prieto Picudo Vino de la Tierra de Castilla Y León Cumal",
"code": "dominio-dostares-prieto-picudo-vino-de-la-tierra-de-castilla-y-leon-cumal-2006",
"region": "Spain > Castilla y León > Vino de la Tierra de Castilla y León",
"winery": "Bischöfliches Priesterseminar Trier",
"winery_id": "bischofliches-priesterseminar-trier",
"varietal": "Prieto Picudo",
"price": "15.89",
"vintage": "2006",
"type": "Red Wine",
"link": "http:\/\/www.snooth.com\/wine\/dominio-dostares-prieto-picudo-vino-de-la-tierra-de-castilla-y-leon-cumal-2006\/",
"tags": "",
"image": "https:\/\/ei.isnooth.com\/multimedia\/d\/0\/4\/image_336595_square.jpeg",
"snoothrank": "n\/a",
"available": 0,
"num_merchants": 0,
"num_reviews": 1
},
{
"name": "Dominio Dostares Prieto Picudo Vino de la Tierra de Castilla Y León Cumal",
"code": "dominio-dostares-prieto-picudo-vino-de-la-tierra-de-castilla-y-leon-cumal-2005",
"region": "Spain > Castilla y León > Vino de la Tierra de Castilla y León",
"winery": "Bischöfliches Priesterseminar Trier",
"winery_id": "bischofliches-priesterseminar-trier",
"varietal": "Prieto Picudo",
"price": "38.99",
"vintage": "2005",
"type": "Red Wine",
"link": "http:\/\/www.snooth.com\/wine\/dominio-dostares-prieto-picudo-vino-de-la-tierra-de-castilla-y-leon-cumal-2005\/",
"tags": "",
"image": "https:\/\/ei.isnooth.com\/multimedia\/1\/d\/a\/image_336596_square.jpeg",
"snoothrank": "n\/a",
"available": 0,
"num_merchants": 0,
"num_reviews": 1
},
{
"name": "The Little Penguin Chardonnay Premier",
"code": "the-little-penguin-chardonnay-premier-2010",
"region": "South East Australia",
"winery": "The Little Penguin",
"winery_id": "the-little-penguin",
"varietal": "Chardonnay",
"price": "11.99",
"vintage": "2010",
"type": "White Wine",
"link": "http:\/\/www.snooth.com\/wine\/the-little-penguin-chardonnay-premier-2010\/",
"tags": "",
"image": "https:\/\/ei.isnooth.com\/multimedia\/2\/c\/4\/image_826282_square.jpeg",
"snoothrank": "n\/a",
"available": 0,
"num_merchants": 0,
"num_reviews": 7
}
]
} |
Thanks. That confirms what I've noticed. Somehow the bug was only exposed for StringValue inside a |
In a project of mine, I am fetching JSON from a third-party API and then attempting to unmarshal the response into a protobuf struct, defined with the type
google.protobuf.Struct
. The API response has escaped values in it and is resulting in the following error:bad value in StructValue for key "image": unrecognized type for Value "\"https:\\/\\/ei.isnooth.com\\/multimedia\\/0\\/2\\/8\\/image_787698_square.jpeg\""
.As a workaround I have had to use the regular
encoding/json
library to unmarshal and then marshal the response again, before passing the data on tojsonpb.Unmarshal
with a protobuf.Further information and relevant code can be found on my posts here:
The text was updated successfully, but these errors were encountered: