3

I am doing development on python and GAE,

When I try to use ProtoRPC for web service, I cannot find a way to let my request contain a json format data in message. example like this:

request format:

{"owner_id":"some id","jsondata":[{"name":"peter","dob":"1911-1-1","aaa":"sth str","xxx":sth int}, {"name":...}, ...]}'       

python:

class some_function_name(messages.Message):
owner_id = messages.StringField(1, required=True)
jsondata = messages.StringField(2, required=True)      #is there a json field instead of StringField?

any other suggestion?

Ton
  • 199
  • 8
  • What is the error message, or the unexpected behavior that you are having when using a StringField? – proppy Feb 08 '12 at 13:14
  • @proppy the error response : {"state": "REQUEST_ERROR", "error_message": "Error parsing ProtoRPC request (Unable to parse request content: Expected type , found {u'name': "peter", u'dob': u'1911-1-1', u'xxx': 'sth str', u'aaa': 111} (type ))"} – Ton Feb 09 '12 at 10:12

1 Answers1

6

What you'd probably want to do here is use a MessageField. You can define your nested message above or within you class definition and use that as the first parameter to the field definition. For example:

class Person(Message):
    name = StringField(1)
    dob = StringField(2)

class ClassRoom(Message):
    teacher = MessageField(Person, 1)
    students = MessageField(Person, 2, repeated=True)

Alternatively:

class ClassRoom(Message):
    class Person(Message):
        ...
    ...

That will work too.

Unfortunately, if you want to store arbitrary JSON, as in any kind of JSON data without knowing ahead of time, that will not work. All fields must be predefined ahead of time.

I hope that it's still helpful to you to use MessageField.

lucemia
  • 6,349
  • 5
  • 42
  • 75
Rafe Kaplan
  • 325
  • 1
  • 6
  • Thanks for your suggestion, what I have modify from your code is add repeated=Ture to allow multiple object, therefore it return as a List object when I use my jsondata field. Thanks=) – Ton Feb 09 '12 at 11:22