Uploaded image for project: 'Java Driver'
  1. Java Driver
  2. JAVA-5423

Serialisation to Shell Syntax is outdated.

    • Type: Icon: Improvement Improvement
    • Resolution: Unresolved
    • Priority: Icon: Unknown Unknown
    • None
    • Affects Version/s: None
    • Component/s: JSON
    • None
    • Java Drivers
    • Hide

      1. What would you like to communicate to the user about this feature?
      2. Would you like the user to see examples of the syntax and/or executable code and its output?
      3. Which versions of the driver/connector does this apply to?

      Show
      1. What would you like to communicate to the user about this feature? 2. Would you like the user to see examples of the syntax and/or executable code and its output? 3. Which versions of the driver/connector does this apply to?

      So, given this code snippet:

      var bson = BsonDocument.parse(someDocumentToParse);
      System.out.println(bson.toJson(JsonWriterSettings.builder()
                              .indent(true)
                              .outputMode(JsonMode.SHELL)
                      .build()));
      

      The driver will generate a String that resembles to the mongosh syntax. To give you an example input/output:

      This:

      var bson = BsonDocument.parse("""
                      {
                                _id: { $oid: "5735040085629ed4fa839473" },
                                code: { $code: "{}" },
                                n: { $numberInt: '10000' },
                                l: { $numberLong: '1355875200000' },
                                d: { $numberDouble: '10.5' },
                                dec: { $numberDecimal: '10.99' },
                                lena: 'olya'
                        }"""
              );
      
              System.out.println(bson.toJson(JsonWriterSettings.builder()
                              .indent(true)
                              .outputMode(JsonMode.SHELL)
                      .build()));
      

      outputs:

      {
        "_id": ObjectId("5735040085629ed4fa839473"),
        "code": {
          "$code": "{}"
        },
        "n": 10000,
        "l": NumberLong("1355875200000"),
        "d": 10.5,
        "dec": NumberDecimal("10.99"),
        "lena": "olya"
      }
      

      This is the behaviour of mongosh:

      mongosh test> EJSON.parse(`{
      ...                           "_id": { "$oid": "5735040085629ed4fa839473" },
      ...                           "code": { "$code": "{}" },
      ...                           "n": { "$numberInt": "10000" },
      ...                           "l": { "$numberLong": "1355875200000" },
      ...                           "d": { "$numberDouble": "10.5" },
      ...                           "dec": { "$numberDecimal": "10.99" },
      ...                           "lena": "olya"
      ...                   }`)
      {
        _id: ObjectId('5735040085629ed4fa839473'),
        code: Code('{}'),
        n: 10000,
        l: 1355875200000,
        d: 10.5,
        dec: Decimal128('10.99'),
        lena: 'olya'
      }
      

            Assignee:
            jeff.yemin@mongodb.com Jeffrey Yemin
            Reporter:
            kevin.mas@mongodb.com Kevin Mas Ruiz
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated: