Skip to content
This repository was archived by the owner on Dec 15, 2025. It is now read-only.

Conversation

@ngicks
Copy link

@ngicks ngicks commented Feb 23, 2023

This is a PR that explores how to fix #657 (comment).

The json:",string" option incorrectly quotes value even when it must not be applied.

If this patch is merged, it would not quote nil, or types other than string, boolean, numeric and pointer types that points to those. Also it would report error correctly.

package main

import (
	"encoding/json"
	"fmt"

	jsoniter "github.com/json-iterator/go"
)

type quote struct {
	S *string `json:",string"`
}

func main() {
	var (
		q   quote
		err error
	)
	for idx, str := range []string{
		`{}`,
		`{"S":"\"234\""}`,
		`{"S":"\"\""}`,
		`{"S":"\""}`,
		`{"S":"234"}`,
		`{"S":234"}`,
		`{"S":"234}`,
	} {
		fmt.Printf("case %d, %s\n", idx, str)
		err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal([]byte(str), &q)
		fmt.Printf("jsoniter err: %+v\n", err)
		err = json.Unmarshal([]byte(str), &q)
		fmt.Printf("json err: %+v\n", err)
		fmt.Println()
	}
}

/*
case 0, {}
jsoniter err: <nil>
json err: <nil>

case 1, {"S":"\"234\""}
jsoniter err: <nil>
json err: <nil>

case 2, {"S":"\"\""}
jsoniter err: <nil>
json err: <nil>

case 3, {"S":"\""}
jsoniter err: main.quote.S: stringModeStringDecoder: expect len(s) >= 2, but found ", error found in #9 byte of ...|{"S":"\""}|..., bigger context ...|{"S":"\""}|...
json err: json: invalid use of ,string struct tag, trying to unmarshal "\"" into string

case 4, {"S":"234"}
jsoniter err: main.quote.S: stringModeStringDecoder: expect ", but found 2, error found in #10 byte of ...|{"S":"234"}|..., bigger context ...|{"S":"234"}|...
json err: json: invalid use of ,string struct tag, trying to unmarshal "234" into string

case 5, {"S":234"}
jsoniter err: main.quote.S: ReadString: expects " or n, but found 2, error found in #6 byte of ...|{"S":234"}|..., bigger context ...|{"S":234"}|...
json err: invalid character '"' after object key:value pair

case 6, {"S":"234}
jsoniter err: main.quote.S: readStringSlowPath: unexpected end of input, error found in #10 byte of ...|{"S":"234}|..., bigger context ...|{"S":"234}|...
json err: unexpected end of JSON input
*/

@ngicks ngicks changed the title Fix string option applying to not applicable Fix ,string option applying to non applicable value / types Feb 23, 2023
@ngicks ngicks force-pushed the fix-string-option-applying-to-not-applicable branch from e57361b to c186b73 Compare February 23, 2023 13:39
@ngicks ngicks force-pushed the fix-string-option-applying-to-not-applicable branch from c186b73 to e66e65e Compare February 24, 2023 09:28
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Recursive struct type causes stack overflow / Handling of overlapping field tagged name is not aligned to std

1 participant