| 1 | // Copyright 2023 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | //go:build !goexperiment.jsonv2 || !go1.25 |
| 6 | |
| 7 | package jsontext |
| 8 | |
| 9 | import ( |
| 10 | "github.com/go-json-experiment/json/internal/jsonflags" |
| 11 | "github.com/go-json-experiment/json/internal/jsonwire" |
| 12 | ) |
| 13 | |
| 14 | // AppendQuote appends a double-quoted JSON string literal representing src |
| 15 | // to dst and returns the extended buffer. |
| 16 | // It uses the minimal string representation per RFC 8785, section 3.2.2.2. |
| 17 | // Invalid UTF-8 bytes are replaced with the Unicode replacement character |
| 18 | // and an error is returned at the end indicating the presence of invalid UTF-8. |
| 19 | // The dst must not overlap with the src. |
| 20 | func AppendQuote[Bytes ~[]byte | ~string](dst []byte, src Bytes) ([]byte, error) { |
| 21 | dst, err := jsonwire.AppendQuote(dst, src, &jsonflags.Flags{}) |
| 22 | if err != nil { |
| 23 | err = &SyntacticError{Err: err} |
| 24 | } |
| 25 | return dst, err |
| 26 | } |
| 27 | |
| 28 | // AppendUnquote appends the decoded interpretation of src as a |
| 29 | // double-quoted JSON string literal to dst and returns the extended buffer. |
| 30 | // The input src must be a JSON string without any surrounding whitespace. |
| 31 | // Invalid UTF-8 bytes are replaced with the Unicode replacement character |
| 32 | // and an error is returned at the end indicating the presence of invalid UTF-8. |
| 33 | // Any trailing bytes after the JSON string literal results in an error. |
| 34 | // The dst must not overlap with the src. |
| 35 | func AppendUnquote[Bytes ~[]byte | ~string](dst []byte, src Bytes) ([]byte, error) { |
| 36 | dst, err := jsonwire.AppendUnquote(dst, src) |
| 37 | if err != nil { |
| 38 | err = &SyntacticError{Err: err} |
| 39 | } |
| 40 | return dst, err |
| 41 | } |