1

I have searched many post here but I still couldn't find the answer yet.

Problem: I want to parse this JSON:

{
  "price": {
    "currency": {
      "class": "com.yintagoka.ad.Currency",
      "id": {
        "class": "org.bson.types.ObjectId",
        "inc": -612166639,
        "machine": -1775927134,
        "new": false,
        "time": 1393682821000,
        "timeSecond": 1393682821
      },
      "code": "USD",
      "longName": "United States Dollar",
      "name": "USD",
      "symbol": null
    },
    "amount": 100
  },
  "title": "title",
  "description": "desc"
}

to become an Object in my Grails server

class House {
    ObjectId id
    String name
    String title
    Price price
    // ... many more
}

class Price {
    BigDecimal amount
    Currency currency
}

class Currency {
    ObjectId id
    String code
    String longName
    String name
    String symbol
}

Grails did almost all the heavy stuff for me, but it can't generate the ObjectId. So I will have Object Currency with all the property name filled with the value but the id will be null.

How can I tell Grails to render the ObjectId as well, preferably automatically.

Note: I don't want to use String for my id.

I also read a post that I can set it manually by request.JSON, but the problem is that I always get an empty map.

def save(House house) {
    print('request = '+request.JSON)
    print('house = '+house)
    print('params = '+params)
}

out:

request = [:]
house = com.yintagoka.ad.House@217b1ead[id=<null>,title=title,description=desc,price=com.yintagoka.ad.Price@1a1b0107[amount=100,currency=com.yintagoka.ad.Currency@62e07ff4[id=<null>,name=USD,longName=United States Dollar,code=USD,symbol=<null>]]
params = [action:delete, controller:house]
Community
  • 1
  • 1
yintagoka
  • 13
  • 1
  • 5
  • 1
    ObjectId() is generated by the driver, if you leave it null it will get filled in before the document is sent to the server to be saved. – Asya Kamsky Mar 02 '14 at 01:55
  • JSON body is already read from the request when you bind the request body to domain object. Body will not be available in request anymore. Domain object binding has to be removed if you want to use `request.JSON`. Make the action method argument less. – dmahapatro Mar 02 '14 at 02:27
  • @AsyaKamsky I want to use the existing data on my server, that is why I don't want to set the id as null – yintagoka Mar 02 '14 at 03:07
  • @dmahapatro yeah! thanks it works, even though Grails can't convert it automatically, I get the raw data. But i will still looking for a better way to do this – yintagoka Mar 02 '14 at 03:33

0 Answers0