Skip to content

jsonpatch.Equal : strange behavior on handling literal unicode string equality #172

@luc-alquier

Description

@luc-alquier

Description

Json string "λJohn" is not equal to "\u03BBJohn" for the function jsonpatch.Equal

I hope to not misinterpret rfc which is stating:

strings: are considered equal if they contain the same number of
Unicode characters and their code points are byte-by-byte equal.

Expected behavior

Equality

Step to reproduce

package main

import (
	"fmt"
	jsonpatch "github.com/evanphx/json-patch"
)

//
func main() {
	original := []byte(`{"name": "λJohn", "age": 24, "height": 3.21}`)
	similar := []byte(`
			{
				"age": 24,
				"height": 3.21,
				"name": "\u03BBJohn"
			}
		`)
	different := []byte(`{"name": "Jane", "age": 20, "height": 3.37}`)

	if jsonpatch.Equal(original, similar) {
		fmt.Println(`"original" is structurally equal to "similar"`)
	}

	if !jsonpatch.Equal(original, different) {
		fmt.Println(`"original" is _not_ structurally equal to "different"`)
	}

}

output:

"original" is _not_ structurally equal to "different"

Test with other implementation

using System;
using System.Text.Json;

using System.Text.Json.JsonDiffPatch;					
public class Program
{
	public static void Main()
	{
		var doc1 = JsonDocument.Parse("""{"name": "λJohn", "age": 24, "height": 3.21}""");
		var doc2 = JsonDocument.Parse("""{  "age": 24,   "height": 3.21, "name": "\u03BBJohn"   }""");
		var equal = doc1.DeepEquals(doc2);
		var textEqual = doc1.DeepEquals(doc2, JsonElementComparison.RawText);
		var semanticEqual = doc1.DeepEquals(doc2, JsonElementComparison.Semantic);	
	    Console.WriteLine($"equal= { equal }");
	    Console.WriteLine($"textEqual= { textEqual }");
	    Console.WriteLine($"semanticEqual= { semanticEqual }");
		
	}
	
}

Output:

equal= True
textEqual= True
semanticEqual= True

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions