JSON serialize can't handle regex patterns

XMLWordPrintableJSON

    • Type: Bug
    • Resolution: Done
    • Priority: Minor - P4
    • 1.1
    • Affects Version/s: 1.0
    • Component/s: None
    • None
    • Environment:
      All
    • None
    • None
    • None
    • None
    • None
    • None
    • None

      I generally log all queries in my development environment, but can't log queries involving regular expressions withour getting a "json can't serialize type " exception thrown.

      I've modified a copy of the source code to include:

      if (o instanceof Pattern)

      { buf.append("/").append(o.toString()).append("/").append(Bytes.patternFlags( ((Pattern)o).flags() )); return; }

      just before the throw at the end of serialize(Object,StringBuilder) in JSON.java and it seems to do the job. I'm not sure if you need an equivalent section in parse (since I'm not sure when json gets parsed)

      I also added a rudmentary unit test in JSONTest.java:

      @org.testng.annotations.Test
      public void testPattern()

      { Pattern pattern = Pattern.compile("^Hello$", Pattern.CASE_INSENSITIVE); assertEquals("/^Hello$/i", JSON.serialize(pattern)); }

      I would try to commit a patch, but I've never done that before, and I use windows and git thinks I've changed file permissions on several of the files.

            Assignee:
            Eliot Horowitz (Inactive)
            Reporter:
            Keith Branton
            None
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: