Пример #1
0
func diffValues(expected, actual interface{}) string {
	differ := gojsondiff.New()

	var diff gojsondiff.Diff

	if ve, ok := expected.(map[string]interface{}); ok {
		if va, ok := actual.(map[string]interface{}); ok {
			diff = differ.CompareObjects(ve, va)
		} else {
			return " (unavailable)"
		}
	} else if ve, ok := expected.([]interface{}); ok {
		if va, ok := actual.([]interface{}); ok {
			diff = differ.CompareArrays(ve, va)
		} else {
			return " (unavailable)"
		}
	} else {
		return " (unavailable)"
	}

	formatter := formatter.NewAsciiFormatter(expected)
	formatter.ShowArrayIndex = true

	str, err := formatter.Format(diff)
	if err != nil {
		return " (unavailable)"
	}

	return "--- expected\n+++ actual\n" + str
}
Пример #2
0
func main() {
	app := cli.NewApp()
	app.Name = "jd"
	app.Usage = "JSON Diff"
	app.Version = "0.0.2"

	app.Flags = []cli.Flag{}

	app.Action = func(c *cli.Context) {
		if len(c.Args()) < 2 {
			fmt.Println("Not enough arguments.\n")
			fmt.Printf("Usage: %s diff json_file\n", app.Name)
			os.Exit(1)
		}

		diffFilePath := c.Args()[0]
		jsonFilePath := c.Args()[1]

		// Diff file
		diffFile, err := ioutil.ReadFile(diffFilePath)
		if err != nil {
			fmt.Printf("Failed to open file '%s': %s\n", diffFilePath, err.Error())
			os.Exit(2)
		}

		// Load Diff file
		um := diff.NewUnmarshaller()
		diffObject, err := um.UnmarshalBytes(diffFile)
		if err != nil {
			fmt.Printf("Failed to load diff file '%s': %s\n", diffFilePath, err.Error())
			os.Exit(2)
		}

		// JSON file
		jsonFile, err := ioutil.ReadFile(jsonFilePath)
		if err != nil {
			fmt.Printf("Failed to open file '%s': %s\n", jsonFilePath, err.Error())
			os.Exit(2)
		}

		// Load JSON
		var jsonObject map[string]interface{}
		json.Unmarshal(jsonFile, &jsonObject)

		// Apply
		differ := diff.New()
		differ.ApplyPatch(jsonObject, diffObject)

		pachedJson, _ := json.MarshalIndent(jsonObject, "", "  ")
		fmt.Println(string(pachedJson))
	}

	app.Run(os.Args)
}
Пример #3
0
	. "github.com/onsi/gomega"

	diff "github.com/gavv/gojsondiff"
)

var _ = Describe("Ascii", func() {
	Describe("AsciiPrinter", func() {
		var (
			a, b map[string]interface{}
		)

		It("Prints the given diff", func() {
			a = LoadFixture("../FIXTURES/base.json")
			b = LoadFixture("../FIXTURES/base_changed.json")

			diff := diff.New().CompareObjects(a, b)
			Expect(diff.Modified()).To(BeTrue())

			f := NewAsciiFormatter(a)
			deltaJson, err := f.Format(diff)
			Expect(err).To(BeNil())
			Expect(deltaJson).To(Equal(
				` {
   "arr": [
     "arr0",
     21,
     {
       "num": 1,
-      "str": "pek3f"
+      "str": "changed"
     },
Пример #4
0
func main() {
	app := cli.NewApp()
	app.Name = "jd"
	app.Usage = "JSON Diff"
	app.Version = "0.0.2"

	app.Flags = []cli.Flag{
		cli.StringFlag{
			Name:   "format, f",
			Value:  "ascii",
			Usage:  "Diff Outpu Format (ascii, delta)",
			EnvVar: "DIFF_FORMAT",
		},
	}

	app.Action = func(c *cli.Context) {
		if len(c.Args()) < 2 {
			fmt.Println("Not enough arguments.\n")
			fmt.Printf("Usage: %s json_file another_json_file\n", app.Name)
			os.Exit(1)
		}

		aFilePath := c.Args()[0]
		bFilePath := c.Args()[1]

		// Prepare your JSON string as `[]byte`, not `string`
		aString, err := ioutil.ReadFile(aFilePath)
		if err != nil {
			fmt.Printf("Failed to open file '%s': %s\n", aFilePath, err.Error())
			os.Exit(2)
		}

		// Another JSON string
		bString, err := ioutil.ReadFile(bFilePath)
		if err != nil {
			fmt.Printf("Failed to open file '%s': %s\n", bFilePath, err.Error())
			os.Exit(2)
		}

		// Then, compare them
		differ := diff.New()
		d, err := differ.Compare(aString, bString)
		if err != nil {
			fmt.Printf("Failed to unmarshal file: %s\n", err.Error())
			os.Exit(3)
		}

		// Output the result
		format := c.String("format")
		var diffString string
		if format == "ascii" {
			var aJson map[string]interface{}
			json.Unmarshal(aString, &aJson)
			formatter := formatter.NewAsciiFormatter(aJson)
			formatter.ShowArrayIndex = true
			diffString, err = formatter.Format(d)
			if err != nil {
				// No error can occur
			}
			fmt.Print(diffString)
		} else if format == "delta" {
			formatter := formatter.NewDeltaFormatter()
			diffString, err = formatter.Format(d)
			if err != nil {
				// No error can occur
			}
		} else {
			fmt.Printf("Unknown Foramt %s\n", format)
			os.Exit(4)
		}

		fmt.Println(diffString)
	}

	app.Run(os.Args)
}