Skip to content

No error reported for invalid raw json with ValidateJsonRawMessage = true #558

Open
@rocurley

Description

@rocurley

Expected result: when attempting to serialize invalid raw json, when ValidateJsonRawMessage = true, an error should be returned.
Actual result: no error is returned. Instead, the invalid raw json is serialized as null.

package main

import (
	"encoding/json"
	"fmt"

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

func main() {
	invalidMessage := json.RawMessage("unescaped string")
	encResult, encErr := json.Marshal(invalidMessage)
	fmt.Println("Standard lib:")
	fmt.Println(string(encResult))
	fmt.Println(encErr)
	iterResult, iterErr := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(invalidMessage)
	fmt.Println("jsoniter:")
	fmt.Println(string(iterResult))
	fmt.Println(iterErr)
}

Result:

Standard lib:

json: error calling MarshalJSON for type json.RawMessage: invalid character 'u' looking for beginning of value
jsoniter:
null
<nil>

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions