Re: [Simple-support] Case insensitive deserialization
Brought to you by:
niallg
|
From: Niall G. <gal...@ya...> - 2011-01-08 02:12:59
|
Hi,
You could apply a convention here, like in CamelCaseStyle you can say
new CamelCase(false, false)
Which will convert PersonName to personName. However, as the XML specification implies, XML is case sensitive, which means PersonName is not equal to personName as an element. Also you may notice in the CamelCaseStyle the setElement and setAttribute methods, this allows for various alternatives. For example
camelCase.setElement("PersonName", "personName")
camelCase.setElement("personname", "personName")
etc..
Case insensitivity is not supported because is not really valid XML.
Niall
--- On Fri, 7/1/11, Greg Milette <gre...@gm...> wrote:
From: Greg Milette <gre...@gm...>
Subject: Re: Re: [Simple-support] Case insensitive deserialization
To: "Niall Gallagher" <gal...@ya...>
Cc: sim...@li...
Received: Friday, 7 January, 2011, 5:22 AM
Hello.
I am still having a problem getting inconsistent cased xml to deserialize. I would really appreciate your help.
For example, in the code below, the SampleObject has a "personName" member variable. When the test code passes in "PersonName" the object cannot deserialize.
One solution that will work every time is to use a Style as I have done below, which will accept "personname" in the xml.
The problem is that using a style means that the xml ALWAYS has to be lowercase. This means that while "personname" will be deserialize, "PersonName" or "personName" will not.
Question: How do I make the Persister deserialize any case? Using the Style and Format alone does not work.
Thanks!
import org.junit.Test;
import org.simpleframework.xml.Element;
import org.simpleframework.xml.Root;
import org.simpleframework.xml.core.Persister;
import org.simpleframework.xml.stream.Format;
import org.simpleframework.xml.stream.Style;
public class TestSimple
{
@Test
public void test()
{
//the name is capital and it doesn't like it!
String withUppercase = "<SampleObject><PersonName>john</PersonName></SampleObject>";
Style anycase = new Style()
{
@Override
public String getElement(String arg0)
{
return arg0.toLowerCase();
}
@Override
public String getAttribute(String arg0)
{
return arg0.toLowerCase();
}
};
Format format = new Format(anycase);
Persister serializer = new Persister(format);
SampleObject output;
try
{
output = (SampleObject)serializer.read(SampleObject.class, withUppercase);
}
catch (Exception e)
{
throw new RuntimeException("failed to deserialize as stream", e);
}
System.out.println("output: " + output.getPersonName());
}
}
@Root(name="SampleObject")
class SampleObject
{
@Element
String personName; //note: personName starts with lowercase!
public String getPersonName()
{
return personName;
}
}
On Wed, Sep 29, 2010 at 2:06 PM, Greg Milette <gre...@gm...> wrote:
Hi,
I was unable to find a test case so I have created my own and included it below.
The xml has "Name" as the attribute, but the class has "name" with lowercase n.
I expect that my Style implementation will cause the framework to succeed in deserializing, but it fails.
Can you kindly suggest the appropriate use of the Style object to achieve my desired effect of case insensitivity?
Thanks!
import org.junit.Test;
import org.simpleframework.xml.Attribute;
import org.simpleframework.xml.Root;
import org.simpleframework.xml.core.Persister;
import org.simpleframework.xml.stream.Format;
import org.simpleframework.xml.stream.Style;
public class TestSample
{
@Test
public void test()
{
//the name is capital and it doesn't like it!
String withUppercase = "<SampleObject Name=\"john\"/>";
Style anycase = new Style(){
@Override
public String getElement(String arg0)
{
return arg0.toLowerCase();
}
@Override
public String getAttribute(String arg0)
{
return arg0.toLowerCase();
}
};
Format format = new Format(anycase);
Persister serializer = new Persister(format);
Object output;
try
{
output = serializer.read(SampleObject.class, withUppercase, false);
}
catch (Exception e)
{
throw new RuntimeException("failed to deserialize as stream", e);
}
}
}
@Root
class SampleObject
{
@Attribute
String name; //note: name is lowercase!
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
}
On Sun, Sep 26, 2010 at 10:11 PM, Niall Gallagher <gal...@ya...> wrote:
Hi,
You can set the style in the Format object, and pass that to the persister. I think there is actually a test case available with the download that has an example of case insensitive deserialization.
Niall
--- On Sun, 26/9/10, gre...@gm... <gre...@gm...> wrote:
From: gre...@gm... <gre...@gm...>
Subject: Re: Re: [Simple-support] Case insensitive deserialization
To: "Niall Gallagher" <gal...@ya...>
Cc: sim...@li...
Received: Sunday, 26 September, 2010, 5:34 AM
I agree that the Style class could work, but the Persister class does not allow me to set a Style.
How do I use a Style in the deserialization process?
Thanks,
Greg
On Sep 26, 2010 8:26am, Greg Milette <gre...@gm...> wrote:
>
> Hi
>
> On Sep 26, 2010 4:48 AM, "Niall Gallagher" gal...@ya...> wrote:
>
>
> Hi,
>
>
>
> Take a look at the Style interface, it should allow you to do this.
>
>
>
> Niall
>
> --- On Fri, 24/9/10, Greg Milette gre...@gm...> wrote:
>
>
>
> From: Greg Milette gre...@gm...>
> Subject: [Simple-support] Case insensitive deserialization
>
> To: sim...@li...
> Received: Friday, 24 September, 2010, 6:29 AM
>
> >
> > How do I make the deserializer case insensitive?
>
> >
> > I've got an element like
> > name-----Inline Attachment Follows-----
>
>
> ------------------------------------------------------------------------------
> Start uncovering the many advantages of virtual appliances
> and start using them to simplify application deployment and
> accelerate your shift to cloud computing.
>
> http://p.sf.net/sfu/novell-sfdev2dev
>
> -----Inline Attachment Follows-----
>
>
> _______________________________________________
> Simple-support mailing list
> Sim...@li...
>
> https://lists.sourceforge.net/lists/listinfo/simple-support
>
>
>
>
>
>
>
|