Model Binding to incoming json

55 views
Skip to first unread message

Dillon

unread,
May 25, 2012, 4:02:50 AM5/25/12
to fubumv...@googlegroups.com
newb warning.

after spending entirely to long I still can't figure out where incoming json gets pushed into the inputmodel for my action. We are using fubucore/mvc 9.4 and We have conneg setup. Initially I thought that hydration using json was in model binding but I'll be damned if I can find it. Basically my issue currenlty is trying to get a route like the following to work with the input model while taking in a json object that is 'labeled'

route: site/api/person/<personid>/Address

inputmodel:

public class IncomingModel
{
   public Guid personid {get;set;}
   public Address address {get;set;}
}

Incoming Json:
{"name":"george","address1":"221 E Cullerton","Address2":"Chicago, IL"}

If I do something more like {"address":{<yada>}} It works fine, but for various reasons I'd like to avoid that.

So how do I bind the incoming json to address?

Also for us uber newbs, if I have the version number off the fubucore/mvc dll (0.9.4.117) how do I translate that to a git revision so I can use it in the debugger?

Thanks and let me know if I flubbed edict somewhere along the way.

Jeremy D. Miller

unread,
May 25, 2012, 7:59:30 AM5/25/12
to fubumv...@googlegroups.com
Dillon,

The json coming in just runs through serialization.  It applies the model binding after the fact just to pick up query string and route values, but primarily it's json serialization.
 
Jeremy D. Miller
The Shade Tree Developer
jeremy...@yahoo.com



From: Dillon <gru...@gmail.com>
To: fubumv...@googlegroups.com
Sent: Fri, May 25, 2012 6:23:46 AM
Subject: [fubumvc] Model Binding to incoming json
--
You received this message because you are subscribed to the Google Groups "FubuMVC Development Group" group.
To view this discussion on the web visit https://groups.google.com/d/msg/fubumvc-devel/-/txfGkKbP8JEJ.
To post to this group, send email to fubumv...@googlegroups.com.
To unsubscribe from this group, send email to fubumvc-deve...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/fubumvc-devel?hl=en.

Dillon

unread,
May 25, 2012, 9:34:26 AM5/25/12
to fubumv...@googlegroups.com
Where is the serialization done?
Any recomendations/workarounds for the scenario given?
Can I get ahold of the actual form data from the request in a custom PropertyBinder and deserialize it again to the object of my choice?

I saw some stuff about json/xml being added to fubumvc binding at some point but I didn't come away with a clear picture of current status. (Not asking for a timeline :)


On Friday, May 25, 2012 6:59:30 AM UTC-5, Jeremy Miller wrote:
Dillon,

The json coming in just runs through serialization.  It applies the model binding after the fact just to pick up query string and route values, but primarily it's json serialization.
 
Jeremy D. Miller
The Shade Tree Developer




Sent: Fri, May 25, 2012 6:23:46 AM
Subject: [fubumvc] Model Binding to incoming json

newb warning.

after spending entirely to long I still can't figure out where incoming json gets pushed into the inputmodel for my action. We are using fubucore/mvc 9.4 and We have conneg setup. Initially I thought that hydration using json was in model binding but I'll be damned if I can find it. Basically my issue currenlty is trying to get a route like the following to work with the input model while taking in a json object that is 'labeled'

route: site/api/person/<personid>/Address

inputmodel:

public class IncomingModel
{
   public Guid personid {get;set;}
   public Address address {get;set;}
}

Incoming Json:
{"name":"george","address1":"221 E Cullerton","Address2":"Chicago, IL"}

If I do something more like {"address":{<yada>}} It works fine, but for various reasons I'd like to avoid that.

So how do I bind the incoming json to address?

Also for us uber newbs, if I have the version number off the fubucore/mvc dll (0.9.4.117) how do I translate that to a git revision so I can use it in the debugger?

Thanks and let me know if I flubbed edict somewhere along the way.

--
You received this message because you are subscribed to the Google Groups "FubuMVC Development Group" group.
To view this discussion on the web visit https://groups.google.com/d/msg/fubumvc-devel/-/txfGkKbP8JEJ.

Ben

unread,
May 25, 2012, 10:17:41 AM5/25/12
to fubumv...@googlegroups.com
There should be an IJSONReader interface thats called into being by structure map.

So find out where your implemenation of that is, and you'll find the place where the deserialization is done.



After deserialization, the model binding applies to the "inflated" object normally.


To view this discussion on the web visit https://groups.google.com/d/msg/fubumvc-devel/-/zQBmEc16dBIJ.

To post to this group, send email to fubumv...@googlegroups.com.
To unsubscribe from this group, send email to fubumvc-deve...@googlegroups.com.

Ben

unread,
May 25, 2012, 10:23:45 AM5/25/12
to fubumv...@googlegroups.com
For the blah/blah/<input>/blah stuff, we have a marker interface that a url policy modifies the routes for.

So for example, if the input model is

class myInputModel : IHaveParentId
{
public guid parentId {get;set;}
}

The input in this case is routed to the parent ID

The interface requires a parentId prop on the implementation.

Michael Dillon

unread,
May 25, 2012, 10:49:29 AM5/25/12
to fubumv...@googlegroups.com
We are currently using the method naming conventions for url data so, Execute_personid(InputModel inputmodel)

In your example how would your action look for a nested url like that (ie /first/second/<someid>/third/)

Ben

unread,
May 25, 2012, 11:39:26 AM5/25/12
to fubumv...@googlegroups.com



We only used routes named Get,Post,Put,Delete.
We are not using the method naming conventions, so I dont' know how those routes are built,
I'm assuming that in that case, the route is first/second/third/<personid>

Which is not what you want.

What you could do (I'm not sure if this is possible, but) is somehow build a urlpolicy that checks the routes for named things like that 
and if there is some attribute or something, have it apply it as a "before" route param.

If you look at the MethodToUrlBuilder.cs (fubumvc.core.registration.conventions), it shows how it is appending the method input and splitting it.
You may want to copy that functionalility and then extend it with the functionaltity you want, and replace it in your registry (service => replace or whatever it is).

Something along those lines should take care of what you are trying to do, you just need to "step in" to the place where its building that route, and instead of appending, you want to put it inside the route itself.

Michael Dillon

unread,
May 25, 2012, 11:50:05 AM5/25/12
to fubumv...@googlegroups.com
Sorry I guess I wasn't clear, getting the route information is not the issue. With our conventions We can support the given url without issue.

The issue is just getting the json object (un 'labeled') to serialize into the other property of the inputmodel.

Ben

unread,
May 25, 2012, 11:59:47 AM5/25/12
to fubumv...@googlegroups.com
Ah, that makes a lot more sense.

Now take this all with salt, as this is my opinion.
I would recommend wrapping it with {address: {blah blah}}, because the json input, (and the input on forms and etc), needs to represent the input model directly.

Now, you could make your input model itself inherit from address, or expect to actually take in an address, instead of a object with an address on it.
This would solve your issue, as the fields would be tied directly to each other, or you could somehow build a jsonreader that would ignore properities on the input model that are in the route name or something strange, but I think you're going to run into a lot more problems that way.

Because in this case, the input model itself is an object that is being taken in, with a property on it called address, the model you are sending in is only representing a single property on the input model.

Michael Dillon

unread,
May 25, 2012, 12:18:14 PM5/25/12
to fubumv...@googlegroups.com
Yeah if I do something like {''address":{"name":"george"}} it works fine, but this endpoint is inteneded to work with default backbone.sync and by default it doesn't wrap the objects, I've looked at it and didn't see an elegant way to shoe horn it in either. It also just kinda bothers me (which is the real issue :)

So I'll be poking about in the json serializer and drooling over the post from miller talking about json being avail at bind time.

Ben

unread,
May 25, 2012, 12:22:57 PM5/25/12
to fubumv...@googlegroups.com
Yea, if you want to customize the serialization for json, you need to replace the IJsonReader service, it shouldn't be too tough to do something in there.

Dillon

unread,
May 25, 2012, 12:26:13 PM5/25/12
to fubumv...@googlegroups.com
Now I've just got to find it :)  I'm not the swiftest at teasing out where stuff is and how to get to it without a debugger.

On that note how do you guys debug fubu? Do you know somehow what release goes with which git commit pull it and compile it then link it in VS or am I missing something simple.

Ben

unread,
May 25, 2012, 12:32:05 PM5/25/12
to fubumv...@googlegroups.com
In the services section of your fubu registry, you can replace services, there should be a default ijsonreader implementation in fubu.

i.e.

Services(configure =>
            {
                configure.ReplaceService<IJsonReader, CustomJsonReader>();
}
Reply all
Reply to author
Forward
0 new messages