Last active
October 7, 2015 03:34
-
-
Save ebot/b2e8436ceb1a1d34186c to your computer and use it in GitHub Desktop.
Example that compares parsing a complex json object in go and in elixir
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
# { "DocVersions": { | |
# "1": { | |
# "version_date": "2015-10-06 12:00:00", | |
# "objects": [ | |
# { "LogicalObjNum":1, | |
# "PhysicalObjNum":1, | |
# "FileExtension":"TIF", | |
# "FileFmtDesc":"TIFF Images (Black and White)"} | |
# ] | |
# } | |
# }, | |
# "DocLabels":[ {"label":"ed"}, {"label":"Ann"}] | |
# } | |
json_string = "{ \"DocVersions\": { \"1\": { \"version_date\": \"2015-10-06 12:00:00\", \"objects\":[ { \"LogicalObjNum\":1, \"PhysicalObjNum\":1, \"FileExtension\":\"TIF\", \"FileFmtDesc\":\"TIFF Images (Black and White)\"} ] } }, \"DocLabels\":[{\"label\":\"Ed\"},{\"label\": \"Ann\"}]}" | |
meta = Poison.Parser.parse!(json_string) | |
[first_label | _] = Map.get meta, "DocLabels" | |
versions = Map.get meta, "DocVersions" | |
IO.puts "Label One: #{first_label["label"]}" | |
IO.puts "Versions:" | |
print_object = fn(o) -> | |
Enum.map(o, fn {k, v} -> IO.puts " #{k}: #{v}" end) | |
end | |
print_version = fn {k, v} -> | |
IO.puts " Version: #{k}" | |
IO.puts " Date: #{v["version_date"]}" | |
IO.puts " Objects:" | |
Enum.map(v["objects"], fn obj -> print_object.(obj) end) | |
end | |
Enum.map(versions, print_version) |
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
package main | |
import ( | |
"encoding/json" | |
"fmt" | |
) | |
func main() { | |
/* | |
{ "DocVersions": { | |
"1": { | |
"version_date": "2015-10-06 12:00:00", | |
"objects": | |
[{ "LogicalObjNum":1, | |
"PhysicalObjNum":1, | |
"FileExtension":"TIF", | |
"FileFmtDesc":"TIFF Images (Black and White)"} | |
] | |
} | |
}, | |
"DocLabels":[{"label":"Ed"}, {"label":"Ann"}] | |
} | |
*/ | |
type Meta struct { | |
DocVersions map[string]struct { | |
Date string `json:"version_date"` | |
Objects []struct { | |
LogicalObjNum int | |
PhysicalObjNum int | |
FileExtension string | |
FileFmtDesc string | |
} `json:"objects"` | |
} | |
DocLabels []struct { | |
Value string `json:"label"` | |
} | |
} | |
var meta Meta | |
jsonString := "{ \"DocVersions\": { \"1\": { \"version_date\": \"2015-10-06 12:00:00\", \"objects\":[ { \"LogicalObjNum\":1, \"PhysicalObjNum\":1, \"FileExtension\":\"TIF\", \"FileFmtDesc\":\"TIFF Images (Black and White)\"} ] } }, \"DocLabels\":[{\"label\":\"Ed\"},{\"label\": \"Ann\"}]}" | |
jsonByteArray := []byte(jsonString) | |
json.Unmarshal(jsonByteArray, &meta) | |
fmt.Println("Label One:", meta.DocLabels[0].Value) | |
fmt.Println("Versions:") | |
for versionNum, version := range meta.DocVersions { | |
fmt.Printf(" Version: %v %s\n", versionNum, version.Date) | |
fmt.Println(" Objects: ") | |
for _, object := range version.Objects { | |
fmt.Printf(" Object %d, Page %d: %s - %s\n", | |
object.PhysicalObjNum, object.LogicalObjNum, object.FileExtension, object.FileFmtDesc) | |
} | |
} | |
} |
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' | |
# { "DocVersions": { | |
# "1": { | |
# "version_date": "2015-10-06 12:00:00", | |
# "objects": [ | |
# { "LogicalObjNum":1, | |
# "PhysicalObjNum":1, | |
# "FileExtension":"TIF", | |
# "FileFmtDesc":"TIFF Images (Black and White)"} | |
# ] | |
# } | |
# }, | |
# "DocLabels":[ {"label":"ed"}, {"label":"Ann"}] | |
# } | |
json_string = "{ \"DocVersions\": { \"1\": { \"version_date\": \"2015-10-06 12:00:00\", \"objects\":[ { \"LogicalObjNum\":1, \"PhysicalObjNum\":1, \"FileExtension\":\"TIF\", \"FileFmtDesc\":\"TIFF Images (Black and White)\"} ] } }, \"DocLabels\":[{\"label\":\"Ed\"},{\"label\": \"Ann\"}]}" | |
doc_meta = JSON.parse json_string | |
puts " Document Labels:" | |
doc_meta['DocLabels'].each do |l| | |
l.each { |lk, lv| puts " #{lv}" } | |
end | |
puts " Document Versions:" | |
doc_meta['DocVersions'].each do |k,v| | |
puts " Version #{k} (#{v["version_date"]}):" | |
v["objects"].each do |object| | |
object.each { |ok, ov| puts " #{ok} => #{ov}" } | |
puts '----------------------------------------------------------' | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment