Skip to content

Instantly share code, notes, and snippets.

@mcgain
Created August 12, 2018 01:31
Show Gist options
  • Select an option

  • Save mcgain/97c9f50c6c2970c361170e5bdb963e4f to your computer and use it in GitHub Desktop.

Select an option

Save mcgain/97c9f50c6c2970c361170e5bdb963e4f to your computer and use it in GitHub Desktop.
require "json"
puts "Hash merging demo"
json_hash = <<-EOS
{
"manifest":["manifest1"],
"current": "string",
"coverage": {
"key": [1,2,null,null,1],
"key2": [0,0,0,null]
},
"job_id": 1
}
EOS
#Trying to avoid null values in the end result
class Line
def initialize(pull_parser : JSON::PullParser)
value = pull_parser.read_int_or_null
if value.nil?
-1
else
value
end
end
end
class Coverage
JSON.mapping(
manifest: Array( String ),
current: String,
job_id: String,
coverage: Array( Line )
)
end
#this is actually what I'd prefer
class BetterCoverage
JSON.mapping(
manifest: Array( String ),
current: String,
job_id: String,
coverage: Array( Int32 | Nil )
)
end
coverage = Coverage.from_json(json_hash)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment