LLBLGen and Json

Posts   
 
    
mdissel
User
Posts: 92
Joined: 16-Sep-2003
# Posted on: 31-Jan-2008 22:56:28   

Hello

I searched in this forum for json, but no results. Nobody used llblgen with some Ajax based website that uses json to load data from the server? If you convert the current IEntity objects to json you get a lot of overhead. I'm only interested in the database / custom fields from the IEntity. I could create a template that creates the minimal classes, but i wonder if anyone else has done this already.

Thanks

Marco

jmeckley
User
Posts: 403
Joined: 05-Jul-2006
# Posted on: 01-Feb-2008 14:34:12   

since your only interested in specific properties, I would map the entity to a dto and json/serialize the dto.

mattsmith321 avatar
Posts: 146
Joined: 04-Oct-2004
# Posted on: 01-Feb-2008 15:39:20   

jmeckley wrote:

since your only interested in specific properties, I would map the entity to a dto and json/serialize the dto.

I agree. And use the latest AjaxPro assembly to get compliant JSON.

mdissel
User
Posts: 92
Joined: 16-Sep-2003
# Posted on: 01-Feb-2008 16:01:18   

jmeckley wrote:

since your only interested in specific properties, I would map the entity to a dto and json/serialize the dto.

Anyone already made a simple DTO template?

jmeckley
User
Posts: 403
Joined: 05-Jul-2006
# Posted on: 01-Feb-2008 16:05:01   

my projects are smaller in size and dto are specific to the gui, so I code dtos manually. search the template studio forum. there are templates for 2.0. if your using 2.5 they need to be tweaked slightly. not sure how, as I'm using 2.0.

JoshWright
User
Posts: 12
Joined: 05-Sep-2008
# Posted on: 05-Sep-2008 02:36:03   

I don't know if you're still looking to serialize LLBLGen entities to json, but I've got a solution that may work for you. It's built for the Self Servicing pattern (version 2.6), but it probably wouldn't be too hard to make it work with Adapter.

Basically there's a couple of extension methods that let you list fields or related entities (with their fields) and it will serialize everything with minimal code.

Here's an example that would serialize the BuyerName and Price for an Order Entity:


//Result:
//{"BuyerName":"Ben Franklin","Price":49.95}

order.ToJSon(
  OrderFields.BuyerName,
  OrderFields.Price
);

Here's a larger example that serializes some fields on Order, then some other fields on the related entities Product, Address, and State:


//Result:
//{"BuyerName":"Ben Franklin","Price":49.95,"Product":{"Name":"Headphones"},"Address":{"City":"Oklahoma City","State":{"Name":"Oklahoma"}}}

order.ToJSon(
    OrderFields.BuyerName,
    OrderFields.Price,
    OrderEntity.Relations.ProductEntityUsingProductId.With(
        ProductFields.Name
    ),
    OrderEntity.Relations.AddressEntityUsingShippingAddressId.With(
        AddressFields.City,
        AddressEntity.Relations.StateEntityUsingStateId.With(
            StateFields.Name
        )
    )
);

No promises about the quality, but it has unit tests and it's worked for me so far. Note that you do have to prefetch whatever you want to serialize (it won't do lazy loading).

swallace
User
Posts: 648
Joined: 18-Aug-2003
# Posted on: 11-Sep-2008 19:04:31   

Josh, that's great code. Thanks for sharing. I hope Otis will make it an inclusion in the user code area.

For others, be sure to seek out Josh's blog for specific implementation details:

http://www.readingwright.com/Post/Show/LLBLGen-to-JSon

tprohas
User
Posts: 257
Joined: 23-Mar-2004
# Posted on: 04-Jun-2009 20:09:48   

Hello all,

I've been using LLBLGen Pro for a while now but don't know how to add an extension to it. Can someone please explain how I go about using this serializer?

BTW, Thank you very much for this code. This is exactly what I've been trying to figure out how to do. simple_smile

trevorg
User
Posts: 104
Joined: 15-Nov-2007
# Posted on: 18-Feb-2010 18:46:41   

Just a heads up on Josh's example....to see the extension method, you must include:

using LLBLGenExtensions.EntityExtensions.JSon;