Skip to content

> character is not decoded properly when applying json patches #198

Closed

Description

@nvelat

The > character is decoded as unicode rather than text after applying a json patch. Here's an example:

package main

import (
	"fmt"

	jsonpatch "github.com/evanphx/json-patch/v5"
)

func main() {
	original := []byte(`{
  "version": "0.1.0",
  "engines": {
    "node": ">= 10.16.3"
  }
}`)
	patchJSON := []byte(`[{"op": "replace", "path": "/version", "value": "0.2.0"}]`)
	patch, err := jsonpatch.DecodePatch(patchJSON)
	if err != nil {
		panic(err)
	}
	modified, err := patch.ApplyIndent(original, "  ")
	if err != nil {
		panic(err)
	}

	fmt.Printf("Original document:\n%s\n", original)
	fmt.Printf("Modified document:\n%s\n", modified)
}

Output:

Original document:
{
  "version": "0.1.0",
  "engines": {
    "node": ">= 10.16.3"
  }
}
Modified document:
{
  "version": "0.2.0",
  "engines": {
    "node": "\u003e= 10.16.3"
  }
}

Activity

  1. evanphx commented on Jan 16, 2024

    @evanphx
    Owner

    The reason for this is that encoding/json escapes for HTML inclusion by default. I'm not sure I want to change that at this stage of the game, it could subtle break anyone that uses the output directly.

  2. nvelat commented on Jan 16, 2024

    @nvelat
    Author

    Hmm, is it possible to get a new ApplyOption that allows us to SetEscapeHTML(false) on the json encoder? That's how you would normally work around this in the encoding/json library

  3. evanphx commented on Jan 28, 2024

    @evanphx
    Owner

    5.9.0 includes a new EscapeHTML value on ApplyOptions you can set to control this behavior.

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

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