[RUBY] Diffed with JSON

I was writing rspec using JSON matcher but I didn't know what was different because there were so many JSON elements

a.rb


#!/usr/bin/env ruby

def main
  ARGF.each do |line|
    line.match(/expected (.*) to be JSON matching (.*)/) do |m|
      e, r = m[1, 2]
      r.gsub!(/(At=>)([^"](?:[^,]+|,[^]|,[^:])+)(,:)/) do
        "#{$1}\"#{$2}\"#{$3}"
      end
      r.gsub!(/(\(an instance of String\))/, "\"\\1\"")
      r = eval(r)
      puts e
      puts r.to_json
    end
  end
end

main
$ rspec aaa_spec.rb:xxx >! out
$ ./a.rb out | jq -s '.[0]' >! b
$ ./a.rb out | jq -s '.[1]' >! c
$ vimdiff b c

I wrote it like this, but in the end I found a partial scratch to identify the cause.

Let's write an article and make a memorial service.

Postscript

It ended up like this

$ perl -lne 'm/^\s+[+-]/ and do { s/^\s+-/e = {/; s/^\s+\+/r = {/; s/,$/}/; s/(At=>)([^"][^,]+)(, :)/\1"\2"\3/g; s/(=>)(\([^)]+\))(,)/\1"\2"\3/g; s/\(contain exactly /[/g and do { s/([}\]"\d])\)/\1]/g; s/ and /, /g;}; print};BEGIN{print"#!/usr/bin/env ruby\nrequire '"'"'json'"'"'\n"}END{print "puts e.to_json\nputs r.to_json"}'

Recommended Posts

Diffed with JSON
Import JSON with SolrJ
JSON validation with JSON schema
Handle JSON with minimal-json
Format JSON with org.json
[Java] JSON communication with jackson
Handle JSON effortlessly with Alamofire's respone Json
Apply regular expression matching with JSON Sassert
Create a GUI JSON Viewer with Ruby/GTK3
Working with huge JSON in Java Lambda
JSON comparison
Convert JSON to TSV and TSV to JSON with Ruby
Create an RSA encryption-enabled JSON API with wicket
Search JPQL for tables with JSON type columns
JSON with Java and Jackson Part 2 XSS measures
Handle JSON in cross domain with Play Framework