Skip to content

Instantly share code, notes, and snippets.

@ddrscott
Last active December 10, 2015 12:48
Show Gist options
  • Select an option

  • Save ddrscott/b501793ba3d29e0fd3e2 to your computer and use it in GitHub Desktop.

Select an option

Save ddrscott/b501793ba3d29e0fd3e2 to your computer and use it in GitHub Desktop.
RubyMotion and JSON marshal bug
it "can marshal" do
json_data = <<-EOD
{
"id": 155778,
"name": "The Bad Beginning: A Series of Unfortunate Events, Book 1",
"contributors": [
["aut", "Author", "Snicket, Lemony"],
["ill", "Illustrator", "Helquist, Brett"]
],
"publisher": "HarperCollins",
"format": "eepub",
"series": "A Series of Unfortunate Events",
"subtitle": null,
"isbns": ["9780061757099", "9780061146305", "9780064407663"],
"synopsis": "<p>Dear Reader,</p><p>I'm sorry to say that the book you are holding in your hands is extremely unpleasant. It tells an unhappy tale about three very unlucky children. Even though they are charming and clever, the Baudelaire siblings lead lives filled with misery and woe. From the very first page of this book when the children are at the beach and receive terrible news, continuing on through the entire story, disaster lurks at their heels. One might say they are magnets for misfortune. </p><p>In this short book alone, the three youngsters encounter a greedy and repulsive villain, itchy clothing, a disastrous fire, a plot to steal their fortune, and cold porridge for breakfast.</p><p>It is my sad duty to write down these unpleasant tales, but there is nothing stopping you from putting this book down at once and reading something happy, if you prefer that sort of thing.</p><p>With all due respect,</p><p>Lemony Snicket</p>",
"excerpts": [
["<p>From the book", "<P>If you are interested in stories with happy endings, you would be better off reading some other book. In this book, not only is there no happy ending, there is no happy beginning and very few happy things in the middle. This is because not very many happy things happened in the lives of the three Baudelaire youngsters. Violet, Klaus, and Sunny Baudelaire were intelligent children, and they were charming, and resourceful, and had pleasant facial features, but they were extremely unlucky, and most everything that happened to them was rife with misfortune, misery, and despair. I'm sorry to tell you this, but that is how the story goes.<p>Their misfortune began one day at Briny Beach. The three Baudelaire children lived with their parents in an enormous mansion at the heart of a dirty and busy city, and occasionally their parents gave them permission to take a rickety trolley-the word \\"rickety,\\" you probably know, here means \\"unsteady\\" or \\"likely to collapse\\"-alone to the seashore, where they would spend the day as a sort of vacation as long as they were home for dinner. This particular morning it was gray and cloudy, which didn't bother the Baudelaire youngsters one bit. When it was hot and sunny, Briny Beach was crowded with tourists and it was impossible to find a good place to lay one's blanket. On gray and cloudy days, the Baudelaires had the beach to themselves to do what they liked.<p>Violet Baudelaire, the eldest, liked to skip rocks. Like most fourteen-year-olds, she was right-handed, so the rocks skipped farther across the murky water when Violet used her right hand than when she used her left. As she skipped rocks, she was looking out at the horizon and thinking about an invention she wanted to build. Anyone who knew Violet well could tell she was thinking hard, because her long hair was tied up in a ribbon to keep it out of her eyes. Violet had a real knack for inventing and building strange devices, so her brain was often filled with images of pulleys, levers, and gears, and she never wanted to be distracted by something as trivial as her hair. This morning she was thinking about how to construct a device that could retrieve a rock after you had skipped it into the ocean.<p>Klaus Baudelaire, the middle child, and the only boy, liked to examine creatures in tidepools. Klaus was a little older than twelve and wore glasses, which made him look intelligent. He was intelligent. The Baudelaire parents had an enormous library in their mansion, a room filled with thousands of books on nearly every subject. Being only twelve, Klaus of course had not read all of the books in the Baudelaire library, but he had read a great many of them and had retained a lot of the information from his readings. He knew how to tell an alligator from a crocodile. He knew who killed Julius Caesar. And he knew much about the tiny, slimy animals found at Briny Beach, which he was examining now.<p>Sunny Baudelaire, the youngest, liked to bite things. She was an infant, and very small for her age, scarcely larger than a boot. What she lacked in size, however, she made up for with the size and sharpness of her four teeth. Sunny was at an age where one mostly speaks in a series of unintelligible shrieks. Except when she used the few actual words in her vocabulary, like \\"bottle,\\" \\"mommy,\\" and \\"bite,\\" most people had trouble understanding what it was that Sunny was saying. For instance, this morning she was saying \\"Gack!\\" over and over, which probably meant, \\"Look at that mysterious figure emerging from the fog!\\"<p>Sure enough, in the distance along the misty shore of Briny Beach there could be seen a tall figure striding toward the Baudelaire children. Sunny had..."]
],
"awards": null,
"reviews": null,
"first_chapter": null,
"sample": null,
"min_age": 8,
"max_age": 18,
"meta_title_id": 420231,
"release_date": "2009-03-17T00:00:00+00:00",
"author_bio": [
["aut", "Author", "<p>Lemony Snicket is often despondent, mostly about his published research, which includes A Series of Unfortunate Events and <i>The Composer Is Dead</i>. </p>"],
["ill", "Illustrator", "<p>Lemony Snicket is often despondent, mostly about his published research, which includes A Series of Unfortunate Events and <i>The Composer Is Dead</i>. </p>"]
],
"subjects": [
["JUV000000", "JUVENILE FICTION"]
],
"prices": [{
"territory": "us",
"agency": "HarperCollins Publishers Inc.",
"selling_price": 1.99,
"suggested_price": 1.99,
"currency": "USD",
"asset_id": 155787
}],
"territory": "us",
"agency": "HarperCollins Publishers Inc.",
"selling_price": 1.99,
"suggested_price": 1.99,
"currency": "USD",
"asset_id": 155787,
"extents": {
"file_size": 1651567,
"language": "English",
"pages": null
},
"seller_statement": "",
"edition_uuid": "4535933e-75c6-4044-92bc-5d3a1676672b",
"thumbnail_url": "http://thumbs.pagefoundry.com/thumbs/6021236-x146.jpg"
}
EOD
# parse JSON to Hash
e = Pointer.new(:object)
parsed_hash = NSJSONSerialization.JSONObjectWithData(json_data.dataUsingEncoding(NSUTF8StringEncoding), options:0, error: e)
raise e[0].userInfo[NSLocalizedDescriptionKey] unless parsed_hash
# generated using MRI Ruby 'pp JSON.parse(json_data)'
expected_hash = {
"synopsis"=>
"<p>Dear Reader,</p><p>I'm sorry to say that the book you are holding in your hands is extremely unpleasant. It tells an unhappy tale about three very unlucky children. Even though they are charming and clever, the Baudelaire siblings lead lives filled with misery and woe. From the very first page of this book when the children are at the beach and receive terrible news, continuing on through the entire story, disaster lurks at their heels. One might say they are magnets for misfortune. </p><p>In this short book alone, the three youngsters encounter a greedy and repulsive villain, itchy clothing, a disastrous fire, a plot to steal their fortune, and cold porridge for breakfast.</p><p>It is my sad duty to write down these unpleasant tales, but there is nothing stopping you from putting this book down at once and reading something happy, if you prefer that sort of thing.</p><p>With all due respect,</p><p>Lemony Snicket</p>",
"subjects"=>[["JUV000000", "JUVENILE FICTION"]],
"meta_title_id"=>420231,
"reviews"=>nil,
"selling_price"=>1.98999977111816,
"publisher"=>"HarperCollins",
"name"=>"The Bad Beginning: A Series of Unfortunate Events, Book 1",
"asset_id"=>155787,
"sample"=>nil,
"thumbnail_url"=>"http://thumbs.pagefoundry.com/thumbs/6021236-x146.jpg",
"isbns"=>["9780061757099", "9780061146305", "9780064407663"],
"edition_uuid"=>"4535933e-75c6-4044-92bc-5d3a1676672b",
"release_date"=>"2009-03-17T00:00:00+00:00",
"territory"=>"us",
"first_chapter"=>nil,
"series"=>"A Series of Unfortunate Events",
"id"=>155778,
"author_bio"=>
# save this for specific comparison
expected_author_bio =
[["aut", "Author", "<p>Lemony Snicket is often despondent, mostly about his published research, which includes A Series of Unfortunate Events and <i>The Composer Is Dead</i>. </p>"],
["ill", "Illustrator", "<p>Lemony Snicket is often despondent, mostly about his published research, which includes A Series of Unfortunate Events and <i>The Composer Is Dead</i>. </p>"]],
"contributors"=>
[["aut", "Author", "Snicket, Lemony"],
["ill", "Illustrator", "Helquist, Brett"]],
"agency"=>"HarperCollins Publishers Inc.",
"seller_statement"=>"",
"suggested_price"=>1.98999977111816,
"excerpts"=>
[["<p>From the book",
"<P>If you are interested in stories with happy endings, you would be better off reading some other book. In this book, not only is there no happy ending, there is no happy beginning and very few happy things in the middle. This is because not very many happy things happened in the lives of the three Baudelaire youngsters. Violet, Klaus, and Sunny Baudelaire were intelligent children, and they were charming, and resourceful, and had pleasant facial features, but they were extremely unlucky, and most everything that happened to them was rife with misfortune, misery, and despair. I'm sorry to tell you this, but that is how the story goes.<p>Their misfortune began one day at Briny Beach. The three Baudelaire children lived with their parents in an enormous mansion at the heart of a dirty and busy city, and occasionally their parents gave them permission to take a rickety trolley-the word \"rickety,\" you probably know, here means \"unsteady\" or \"likely to collapse\"-alone to the seashore, where they would spend the day as a sort of vacation as long as they were home for dinner. This particular morning it was gray and cloudy, which didn't bother the Baudelaire youngsters one bit. When it was hot and sunny, Briny Beach was crowded with tourists and it was impossible to find a good place to lay one's blanket. On gray and cloudy days, the Baudelaires had the beach to themselves to do what they liked.<p>Violet Baudelaire, the eldest, liked to skip rocks. Like most fourteen-year-olds, she was right-handed, so the rocks skipped farther across the murky water when Violet used her right hand than when she used her left. As she skipped rocks, she was looking out at the horizon and thinking about an invention she wanted to build. Anyone who knew Violet well could tell she was thinking hard, because her long hair was tied up in a ribbon to keep it out of her eyes. Violet had a real knack for inventing and building strange devices, so her brain was often filled with images of pulleys, levers, and gears, and she never wanted to be distracted by something as trivial as her hair. This morning she was thinking about how to construct a device that could retrieve a rock after you had skipped it into the ocean.<p>Klaus Baudelaire, the middle child, and the only boy, liked to examine creatures in tidepools. Klaus was a little older than twelve and wore glasses, which made him look intelligent. He was intelligent. The Baudelaire parents had an enormous library in their mansion, a room filled with thousands of books on nearly every subject. Being only twelve, Klaus of course had not read all of the books in the Baudelaire library, but he had read a great many of them and had retained a lot of the information from his readings. He knew how to tell an alligator from a crocodile. He knew who killed Julius Caesar. And he knew much about the tiny, slimy animals found at Briny Beach, which he was examining now.<p>Sunny Baudelaire, the youngest, liked to bite things. She was an infant, and very small for her age, scarcely larger than a boot. What she lacked in size, however, she made up for with the size and sharpness of her four teeth. Sunny was at an age where one mostly speaks in a series of unintelligible shrieks. Except when she used the few actual words in her vocabulary, like \"bottle,\" \"mommy,\" and \"bite,\" most people had trouble understanding what it was that Sunny was saying. For instance, this morning she was saying \"Gack!\" over and over, which probably meant, \"Look at that mysterious figure emerging from the fog!\"<p>Sure enough, in the distance along the misty shore of Briny Beach there could be seen a tall figure striding toward the Baudelaire children. Sunny had..."]],
"currency"=>"USD",
"format"=>"eepub",
"subtitle"=>nil,
"awards"=>nil,
"min_age"=>8,
"max_age"=>18,
"extents"=>{"language"=>"English", "file_size"=>1651567, "pages"=>nil},
"prices"=>
[{"currency"=>"USD",
"asset_id"=>155787,
"agency"=>"HarperCollins Publishers Inc.",
"selling_price"=>1.98999977111816,
"suggested_price"=>1.98999977111816,
"territory"=>"us"}]
}
puts "--"
# every parsed key/value pair should match expected
puts "Checking parsed against expected..."
parsed_hash.each do |k, v|
expected_hash[k].to_s.should.equal v.to_s
end
# dump/load the expected_hash
puts "Checking marshal with expected_hash..."
marshalled_expected_hash = Marshal::load(Marshal::dump(expected_hash))
expected_hash.each do |k, v|
marshalled_expected_hash[k].to_s.should.equal v.to_s
end
# dump/load the parsed_hash
marshalled_hash = Marshal::load(Marshal::dump(parsed_hash))
puts "Checking author_bio against expected..."
marshalled_hash['author_bio'].should.equal expected_author_bio
puts "Checking marshalled against expected..."
parsed_hash.should.equal marshalled_hash
parsed_hash.each do |k, v|
marshalled_hash[k].should.equal v
end
end
@ddrscott
Copy link
Author

ddrscott commented Jan 2, 2013

The parsed_hash is correct prior to Marshal::load/dump.

After the Marshal::load/dump, the marshalled_hash exhibits the corruption.

Output

  - can marshal(why doesn't spec tell me line numbers?!?)
Checking parsed against expected...
Checking author_bio against expected...
 [FAILED]

Bacon::Error: [["aut", "Author", "<p>Lemony Snicket is often despondent, mostly about his published research, which includes A Series of Unfortunate Events and <i>The Composer Is Dead</i>. </p>"], ["ill", "Illustrator", "Author"]].==([["aut", "Author", "<p>Lemony Snicket is often despondent, mostly about his published research, which includes A Series of Unfortunate Events and <i>The Composer Is Dead</i>. </p>"], ["ill", "Illustrator", "<p>Lemony Snicket is often despondent, mostly about his published research, which includes A Series of Unfortunate Events and <i>The Composer Is Dead</i>. </p>"]]) failed
spec.rb:553:in `satisfy:': Application 'BrunoIOS' - can marshal
spec.rb:567:in `method_missing:'
 5:in `equal:'
spec.rb:183:in `block in run_spec_block'
spec.rb:307:in `execute_block'
spec.rb:183:in `run_spec_block'
spec.rb:198:in `run'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment