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

json: support decoding non-string values in a string context #202

Closed
juanvallejo opened this issue Aug 21, 2017 · 8 comments
Closed

json: support decoding non-string values in a string context #202

juanvallejo opened this issue Aug 21, 2017 · 8 comments

Comments

@juanvallejo
Copy link

Right now, the codec does not appear to support booleans as values; it either expects an object, array, number, or string, but not boolean values true or false (without quotes).

This would make the following valid json return an error:

{
   "key": true
}

Please consider updating the codec to comply with newer versions of the spec, such as RFC 7159 (see section 3)

@ugorji
Copy link
Owner

ugorji commented Aug 21, 2017

Please provide a reproducer (my_test.go or main.go) that I can run using either "go test my_test.go" or "go run main.go". Also include what you see, and what you expected to see on execution.

Thanks.

@ugorji
Copy link
Owner

ugorji commented Aug 22, 2017

This has always, and continues to work. I suspect there is an error in your code. A reproducer will make this clear. Closing this until a reproducer is provided.

@ugorji ugorji closed this as completed Aug 22, 2017
@juanvallejo
Copy link
Author

Renaming this issue to be in-line with #203
Reproducer:

package main

import (
	"fmt"

	"github.com/ugorji/go/codec"
)

var jsonData = `{
    "selector":  {
        "key1": false
    }
}`

type SampleSchema struct {
	Selector map[string]string `json:"selector"`
}

func main() {
	obj := &SampleSchema{
		Selector: make(map[string]string),
	}

	err := codec.NewDecoderBytes([]byte(jsonData), new(codec.JsonHandle)).Decode(obj)
	if err != nil {
		// attempt to reproduce error:
		// error: [pos 38]: json: expect char '"' but got char 'f'
		fmt.Printf("error: %v\n", err)
	}
}

@juanvallejo juanvallejo changed the title Update codec to comply with newer versions of JSON spec (e.g. 7159) Codec does not support decoding boolean values as strings Aug 25, 2017
@ugorji
Copy link
Owner

ugorji commented Aug 25, 2017

Got it. Let me think about this today.

I see where you are coming from, and can mentally make a case that we can generalize a lenient form of decoding strings, where strings that are not in quotes can be read if a LenientString flag is set in the JsonHandle.

I should have a more concrete response to you by tomorrow.

@ugorji ugorji reopened this Aug 25, 2017
@ugorji
Copy link
Owner

ugorji commented Aug 25, 2017

By definition, this will allow false there to the following:

{ "key": false32ijr, "key2": "false32ijr", "key3": false, "key4": "false" } which is a superset of what you want, but is a fair Lenient flag support.

Let me know if this idea works for you - I think it does, but need your confirmation or color if something is off.

@ugorji ugorji changed the title Codec does not support decoding boolean values as strings support decoding boolean values in a string context Aug 25, 2017
@juanvallejo
Copy link
Author

juanvallejo commented Aug 25, 2017

By definition, this will allow false there to the following:

{ "key": false32ijr, "key2": "false32ijr", "key3": false, "key4": "false" } which is a superset of what you want

@ugorji while I'd be in favor of this, I don't think it would conform to the json spec, given that the only "literals" allowed are true, false, and null https://tools.ietf.org/html/rfc7159#section-3

I have been discussing this with @fabianofranz but will try to get additional opinions as well

@ugorji
Copy link
Owner

ugorji commented Aug 25, 2017

Ah - got it.

You want to be able to parse a "valid" json file, but be flexible based on the destination e.g. if you see a boolean but expect a string, handle appropriately. To be general, I need to support any valid non-string scalar i.e. null, true, false and numbers.

Makes sense to me.

Will follow up.

@ugorji ugorji changed the title support decoding boolean values in a string context json: support decoding non-string values in a string context Aug 25, 2017
@ugorji ugorji closed this as completed in 8c0409f Aug 26, 2017
@ugorji
Copy link
Owner

ugorji commented Aug 26, 2017

^^ @juanvallejo fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants