Created
August 12, 2018 01:31
-
-
Save mcgain/97c9f50c6c2970c361170e5bdb963e4f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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