Skip to content

Inconsistent behaviour when keys include backslash #3

Description

@elliottinvent

Using Ruby's irb:

% irb

Require the gem:

3.0.0 :004"> require "compactdata"
 => true 

Tests 1-3:

3.0.0 :009"> CompactData.generate({"a\\t":1})
 => "a\\t=1" 
3.0.0 :010 > CompactData.generate({"a\t":1})
 => "a\t=1" 
3.0.0 :011"> CompactData.generate({"a\d":1})
 => "ad=1" 

1.\t is a control character (tab) so the double backslash is retained, this would seem correct behaviour.
2.\t (tab) would be retained in the key, which would seem correct behaviour.
3. \d does not represent a control character so Ruby strips out the backslash when it creates the hash and before it gets passed to CompactData.generate, this is demonstrated here:

3.0.0 :012 > {"a\d":1}
 => {:ad=>1} 

In the following example, we create a hash with one pair; the key is key\\key and value is value\\value

This is the output from JSON.generate

{"key\\key":"value\\value"}

This is the output from CompactData.generate:

key\key=value\value

Here are some questions for consideration:

  1. Should the backslash in the CompactData string be escaped too?
  2. Should we ban control characters in keys?
  3. Should we consider every key a quoted string?

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions