Re: [Simple-support] Handling deserialization exceptions
Brought to you by:
niallg
|
From: Митя Б. <dn...@gm...> - 2012-06-12 04:59:18
|
Thanks Niall!
In this simple example - yes, that would be a pretty straightforward way to
achieve it.
But in real life it is much more complex, not just 1 object with 2 fields,
but tens and hundreds, and the server sometimes sends me an epic bullshit,
I simply can't predict, where the exception can occur.
And that's the case the exceptions were made for! You simply put "try"
around the suspicious code, no matter how complex it is, and in the end you
just have "Ok" or "Exception" that you can handle.
If I do all the validation by hand, this would lead me to a clumsy
unreadable error-prone code, that I wanted to avoid and that's why I've
chosen your framework.
It would be great if Simple Framework supported something like this:
@Root(name="order")
class Order {
@Attribute(name="id", required=true)
int id;
@Element(name="name", required=false)
String name;
@HandleException
public boolean handleException(Exception e) {
Log.w(TAG, "Cannot parse Order, " + e);
return false; // false because we don't want to insert it into the
list
// Or we can throw it again
}
}
// Or, the non-intrusive way
@ElementList(stopOnValidationErrors = false)
List<Order> orders;
Wouldn't that be too insolent of me to suggest a new feature? :)
I'll try to add support for it and then show you the result.
But if you think that's unreasonable, please let me know and prevent me
from digging into the code :)
Thanks in advance!
2012/6/12 Niall Gallagher <gal...@ya...>
> the order attribute must have required=false, take a look at the @Validate
> annotation, you can simply remove any orders that have a null id.
>
> --- On *Mon, 11/6/12, Митя Балахонский <dn...@gm...>* wrote:
>
>
> From: Митя Балахонский <dn...@gm...>
> Subject: [Simple-support] Handling deserialization exceptions
> To: sim...@li...
> Received: Monday, 11 June, 2012, 11:28 AM
>
> Hello, dear All
>
> I'm very happy that such a thing as Simple Framework exists, it helps
> me a lot :)
> But now I have an issue:
> I'm using XML for client-server data exchange, and server doesn't
> always send me valid data.
> For example:
>
> <response>
> <order id="123">
> <name>Foo</name>
> </order>
> <order>
> <name>Bar</name>
> </order>
> <order id="456">
> <name>Alice</name>
> </order>
> </response>
>
> I have a simple class for it:
>
> @Root(name="order")
> class Order {
> @Attribute(name="id", required=true)
> int id;
> @Element(name="name", required=false)
> String name;
> }
>
> @Root(name="response")
> class Response {
> @ElementList
> List<Order> orders;
> }
>
> And an @ElementList of Order in the Response class.
>
> It works perfectly until server sends me some order without the "id"
> attribute. In this case parsing fails and I have no way to receive
> other orders that were correctly formed, because framework exception
> fires and cancels all the parsing.
> Is there a proper way to handle it with Simple Framework?
> I'd like to have a list of orders, which were parsed correctly, and
> ignore incorrect ones. With my example that would be a list, containing
> Foo and
> Alice, maybe having some notifications that 1 order cannot be parsed
> because of the exception.
>
> Thanks in advance!
>
> --
> Best regards,
> Dmitry Balakhonsky
>
>
> ------------------------------------------------------------------------------
> Live Security Virtual Conference
> Exclusive live event will cover all the ways today's security and
> threat landscape has changed and how IT managers can respond. Discussions
> will include endpoint security, mobile security and the latest in malware
> threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
> _______________________________________________
> Simple-support mailing list
> Sim...@li...<http://au.mc1610.mail.yahoo.com/mc/compose?to=...@li...>
> https://lists.sourceforge.net/lists/listinfo/simple-support
>
>
--
Best regards,
Dmitry Balakhonsky
|