groovy
  1. groovy
  2. GROOVY-5169

JsonOutput.toJson(object) is not returning expected results

    Details

    • Type: Bug Bug
    • Status: Open Open
    • Priority: Minor Minor
    • Resolution: Unresolved
    • Affects Version/s: 1.8.4
    • Fix Version/s: None
    • Component/s: JSON
    • Labels:
    • Number of attachments :
      6

      Description

      Consider the attached Groovy script. It defines two classes. Each has public properties explicitly defined. When I send the resulting objects to JsonOutput.toJson(), I expect the public properties of the object to be serialized into the JSON output. This is not working as expected. The only properties that get serialized are those created via "def propName" and those that seem to have getter methods (this in not actually the case).

      Attached is a screenshot of GroovyConsole running the attached script.

      1. 0001-bugfix.patch
        2 kB
        umang bhatt
      2. json_test.groovy
        0.8 kB
        James Sumners
      3. MetaClassImpl.java
        151 kB
        umang bhatt
      4. MetaClassImpl.patch
        2 kB
        blackdrag blackdrag
      1. GroovyConsole_output.png
        275 kB
      2. screenshot.png
        67 kB

        Activity

        Hide
        James Sumners added a comment -

        "Also there is the question to answer if a property that consists only of a getter or setter and no field should be included. And dynamically added properties are up to discussion as well I think."

        When I originally filed the bug, and this still hold true for me, I assumed that the instance of the object would be serialized in such a fashion that it could be recreated from the JSON. That would imply dynamically added properties should be included in the serialization. Otherwise, what are you serializing?

        Show
        James Sumners added a comment - "Also there is the question to answer if a property that consists only of a getter or setter and no field should be included. And dynamically added properties are up to discussion as well I think." When I originally filed the bug, and this still hold true for me, I assumed that the instance of the object would be serialized in such a fashion that it could be recreated from the JSON. That would imply dynamically added properties should be included in the serialization. Otherwise, what are you serializing?
        Hide
        blackdrag blackdrag added a comment -

        but if you want this to recreate the object afterwards, then read-only-properties are going to be a problem for you - plus dynamically added properties will only be available if they have been added on the deserialization side as well.

        Show
        blackdrag blackdrag added a comment - but if you want this to recreate the object afterwards, then read-only-properties are going to be a problem for you - plus dynamically added properties will only be available if they have been added on the deserialization side as well.
        Hide
        James Sumners added a comment -

        Well, since Groovy doesn't deserialize JSON to an object anyway (that I can tell) that's a moot point. It would be left up to the deserializer as to how to handle that.

        Show
        James Sumners added a comment - Well, since Groovy doesn't deserialize JSON to an object anyway (that I can tell) that's a moot point. It would be left up to the deserializer as to how to handle that.
        Hide
        James Sumners added a comment -

        By the way, back when I filed this bug I ended up resorting to some ridiculousness to get around it – http://stackoverflow.com/questions/7853809/iterating-an-objects-own-properties-in-groovy

        Show
        James Sumners added a comment - By the way, back when I filed this bug I ended up resorting to some ridiculousness to get around it – http://stackoverflow.com/questions/7853809/iterating-an-objects-own-properties-in-groovy
        Hide
        Richard Hightower added a comment -

        I am lost in the comment stream. What works? What is broken?

        Show
        Richard Hightower added a comment - I am lost in the comment stream. What works? What is broken?

          People

          • Assignee:
            Unassigned
            Reporter:
            James Sumners
          • Votes:
            0 Vote for this issue
            Watchers:
            7 Start watching this issue

            Dates

            • Created:
              Updated: