schematics icon indicating copy to clipboard operation
schematics copied to clipboard

Define JsonType

Open matusbarabas opened this issue 6 years ago • 5 comments

Can someone help me how to create new JsonType ? My column in databese has json type value.

matusbarabas avatar Nov 11 '18 19:11 matusbarabas

dont do that.

call to_primitive(), then call use a function to convert that to json

On Sun, Nov 11, 2018, 2:44 PM Matúš Barabás <[email protected] wrote:

Can someone help me how to create new JsonType ? My column in databese has json type value.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/schematics/schematics/issues/577, or mute the thread https://github.com/notifications/unsubscribe-auth/AADBiA4au7GYE3iK0qKv7ogZwICLUY6Dks5uuH4WgaJpZM4YYpda .

j2labs avatar Nov 11 '18 19:11 j2labs

Can you explain more? What type i should use ? My custom or BaseType? and why i have to call to_primitive function?

matusbarabas avatar Nov 11 '18 19:11 matusbarabas

Just use a model.

Consider that Schematics is not far from being a typed dictionary already.

The only thing left for you to do is to call json.dumps

You can have a model with a field that is another model, which how I would approach your usecase

On Sun, Nov 11, 2018, 2:51 PM Matúš Barabás <[email protected] wrote:

Can you explain more? What type i should use ? My custom or BaseType? and why i have to call to_primitive function?

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/schematics/schematics/issues/577#issuecomment-437699105, or mute the thread https://github.com/notifications/unsubscribe-auth/AADBiMYeoWWBen3xRQN0uVQtQOviOO9-ks5uuH--gaJpZM4YYpda .

j2labs avatar Nov 11 '18 19:11 j2labs

class CreateRequest(Model):
    id: IntType = IntType(required=False)
    param: StringType = StringType(validators=[json_validator])

This is model, which I used. So, I have to change StringType to Model like this?

class CreateRequest(Model):
    id: IntType = IntType(required=False)
    param: Model

And where to use json dumps method. It is still not clear for me.

matusbarabas avatar Nov 11 '18 20:11 matusbarabas

uhh what?! json as a validator?

the whole point of the to_primitive and to_native functions is to convert data to and form python native form to a form that is acceptable to language agnostic encodings, like json

validators are intended to check if the data looks the way the type requires

your code sample appears to conflate these two concepts, tho I cant be sure

i urge you to read the docs and get a good mental model for schematics in your head. the gist of your second sample is going in the right direction, but is not yet correct. the docs will get you there.

On Sun, Nov 11, 2018, 3:07 PM Matúš Barabás <[email protected] wrote:

class CreateRequest(Model): id: IntType = IntType(required=False) param: StringType = StringType(validators=[json_validator])

This is model, which I used. So, I have to change StringType to Model like this?

class CreateRequest(Model): id: IntType = IntType(required=False) param: Model

And where to use json dumps method. It is still not clear for me.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/schematics/schematics/issues/577#issuecomment-437700364, or mute the thread https://github.com/notifications/unsubscribe-auth/AADBiDscXjFRy495pwCYtyvTy52a6Bljks5uuIN-gaJpZM4YYpda .

j2labs avatar Nov 11 '18 20:11 j2labs