Uploaded image for project: 'Jenkins'
  1. Jenkins
  2. JENKINS-53015

Objects returned by readJSON are not plain Java objects

    XMLWordPrintable

    Details

    • Similar Issues:

      Description

      The documentation of readJson suggests that readJson returns plain Java objects (like readYaml does), but they are of type net.sf.json.JSON and some members of that are not whitelisted, so it is painful to work with...

        Attachments

          Activity

          Hide
          matihost matihost matihost added a comment - - edited

          readJSON uses json library released last time 8 years ago - which in IT world means abandoned software

          Why groovy.json.JsonParser is not used?

           

          https://stackoverflow.com/questions/19424490/a-not-null-string-with-literal-value-null-not-handled-correctly-bug-in-json

          no answer for that - but readJSON is impacted  for example it treats JSON value null as "null" string

           

          People recommending abandoning json-lib 5 years ago

           

          Show
          matihost matihost matihost added a comment - - edited readJSON uses json library released last time 8 years ago - which in IT world means abandoned software Why groovy.json.JsonParser is not used?   https://stackoverflow.com/questions/19424490/a-not-null-string-with-literal-value-null-not-handled-correctly-bug-in-json no answer for that - but readJSON is impacted  for example it treats JSON value null as "null" string   People recommending abandoning json-lib 5 years ago  
          Hide
          loganmzz Logan Mzz added a comment -

          As JSON content can be read by YAML parsers, I switched to readYaml

          Show
          loganmzz Logan Mzz added a comment - As JSON content can be read by YAML parsers, I switched to readYaml
          Hide
          tgr Tobias Gruetzmacher added a comment -

          This has been fixed with release 2.4.0 released about a year ago. Sorry for not closing the issue (to keep the plugin backward-compatible, you need to add `returnPojo: true` to the call).

          Show
          tgr Tobias Gruetzmacher added a comment - This has been fixed with release 2.4.0 released about a year ago. Sorry for not closing the issue (to keep the plugin backward-compatible, you need to add `returnPojo: true` to the call).
          Hide
          loganmzz Logan Mzz added a comment -

          I had issue with latest Jenkins version. I will try to submit a MVCE.

          Show
          loganmzz Logan Mzz added a comment - I had issue with latest Jenkins version. I will try to submit a MVCE.
          Hide
          loganmzz Logan Mzz added a comment -

           

          Demo pipeline :

          def printValue(name, value) {
              echo "${name} = ${value}"
              echo "${name}.class = ${value.getClass()}"
          }
          
          def printStruct(array) {
              printValue "array", array
              def object = array[0]
              printValue "object", object
              def id = object.id
              printValue "id", id
              def content = object.content
              printValue "content", content
              printValue "null ?", content == null
          }
          
          def text = """
          [
              {
                  id: 1,
                  content: null
              }
          ]
          """
          
          pipeline {
              agent any
              
              stages {
                  stage('Main') {
                      parallel {
                          stage('readJSON') {
                              steps {
                                  script {
                                      printStruct(readJSON(text: text))
                                  }
                              }
                          }
                          stage('readYaml') {
                              steps {
                                  script {
                                      printStruct(readYaml(text: text))
                                  }
                              }
                          }
                      }
                  }
              }
          }

           

          Outputs :

          • readJSON

           

          array = [[id:1, content:null]]
          array.class = class net.sf.json.JSONArray
          object = [id:1, content:null]
          object.class = class net.sf.json.JSONObject
          id = 1
          id.class = class java.lang.Integer
          content = null
          content.class = class net.sf.json.JSONNull
          null ? = false
          
          • readYaml
          array = [[id:1, content:null]]
          array.class = class java.util.ArrayList
          object = [id:1, content:null]
          object.class = class java.util.LinkedHashMap
          id = 1
          id.class = class java.lang.Integer
          content = null
          content.class = class org.codehaus.groovy.runtime.NullObject
          null ? = true
          

           

          Show
          loganmzz Logan Mzz added a comment -   Demo pipeline : def printValue(name, value) { echo "${name} = ${value}" echo "${name}.class = ${value.getClass()}" } def printStruct(array) { printValue "array" , array def object = array[0] printValue "object" , object def id = object.id printValue "id" , id def content = object.content printValue "content" , content printValue " null ?" , content == null } def text = """ [ { id: 1, content: null } ] """ pipeline { agent any stages { stage( 'Main' ) { parallel { stage( 'readJSON' ) { steps { script { printStruct(readJSON(text: text)) } } } stage( 'readYaml' ) { steps { script { printStruct(readYaml(text: text)) } } } } } } }   Outputs : readJSON   array = [[id:1, content: null ]] array.class = class net.sf.json.JSONArray object = [id:1, content: null ] object.class = class net.sf.json.JSONObject id = 1 id.class = class java.lang. Integer content = null content.class = class net.sf.json.JSONNull null ? = false readYaml array = [[id:1, content: null ]] array.class = class java.util.ArrayList object = [id:1, content: null ] object.class = class java.util.LinkedHashMap id = 1 id.class = class java.lang. Integer content = null content.class = class org.codehaus.groovy.runtime.NullObject null ? = true  
          Hide
          tgr Tobias Gruetzmacher added a comment -

          Of course. For backward compatibility, legacy JSON objects are returned if you omit the returnPojo: true option.

          Show
          tgr Tobias Gruetzmacher added a comment - Of course. For backward compatibility, legacy JSON objects are returned if you omit the returnPojo: true option.

            People

            Assignee:
            rsandell rsandell
            Reporter:
            tgr Tobias Gruetzmacher
            Votes:
            2 Vote for this issue
            Watchers:
            3 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved: