Skip to content

Add encoding.NormalizeJSON and wrapper.WithNormalizedBody(any)#11

Merged
pnguyen215 merged 10 commits intomasterfrom
copilot/review-w-json-functions
Mar 2, 2026
Merged

Add encoding.NormalizeJSON and wrapper.WithNormalizedBody(any)#11
pnguyen215 merged 10 commits intomasterfrom
copilot/review-w-json-functions

Conversation

Copy link
Contributor

Copilot AI commented Mar 1, 2026

JSON strings containing literal \" escape artifacts (e.g. from Go raw string literals or double-encoded transports) are structurally invalid and silently break IsJSONBody(), JSON(), JSONPretty(), and QueryJSONBody(). This adds a normalization pipeline to detect and repair such inputs before processing.

encoding.NormalizeJSON(s string) (string, error)

Multi-pass normalizer with early-exit validation after each pass:

Pass Artifact
1 Strip leading UTF-8 BOM (0xEF 0xBB 0xBF)
2 Remove embedded null bytes (0x00)
3 Unescape literal \"" structural quote artifacts
4 Strip trailing commas before } / ]
  • Fast path: returns the input unchanged (zero allocation) if it is already valid JSON
  • Package-level compiled regexp for Pass 4; Pass 3 guards with strings.Contains before allocating

(*wrapper).WithNormalizedBody(v any) (*wrapper, error)

Accepts any Go value and routes by type:

Input Handling
string NormalizeJSON pass chain
[]byte NormalizeJSON after string conversion
json.RawMessage Direct validation, set as string
any other type encoding.JSONToken marshal
nil Error

Usage

const raw = `{
    \"store\": {
        "owner": "Alice"
    },
    "ratings": [5, 3, 4,]
}`

w, err := replify.New().WithNormalizedBody(raw)
// err == nil
// w.IsJSONBody()                                    → true
// w.QueryJSONBody("store.owner").Get("@upper")      → "ALICE"
// w.JSON() / w.JSONPretty()                         → well-formed JSON

// Struct input also works
w, err = replify.New().WithNormalizedBody(myStruct)

✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Co-authored-by: pnguyen215 <39581473+pnguyen215@users.noreply.github.com>
Copilot AI changed the title [WIP] Review and fix issues in w.JSON and w.JSONPretty functions Fix w.JSON() / w.JSONPretty() double-encoding valid JSON body as escaped string Mar 1, 2026
Co-authored-by: pnguyen215 <39581473+pnguyen215@users.noreply.github.com>
Copilot AI changed the title Fix w.JSON() / w.JSONPretty() double-encoding valid JSON body as escaped string Add encoding.NormalizeJSON and wrapper.WithNormalizedBody to fix escaped-quote JSON artifacts Mar 1, 2026
Co-authored-by: pnguyen215 <39581473+pnguyen215@users.noreply.github.com>
Copilot AI changed the title Add encoding.NormalizeJSON and wrapper.WithNormalizedBody to fix escaped-quote JSON artifacts Add encoding.NormalizeJSON and wrapper.WithNormalizedBody(any) Mar 1, 2026
@pnguyen215 pnguyen215 merged commit de0cbca into master Mar 2, 2026
9 checks passed
@pnguyen215 pnguyen215 deleted the copilot/review-w-json-functions branch March 2, 2026 15:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants