Message from discussion
: Re: Default Value when deserialized?
Received: by 10.50.213.98 with SMTP id nr2mr2756388igc.5.1340414592256;
Fri, 22 Jun 2012 18:23:12 -0700 (PDT)
X-BeenThere: google-gson@googlegroups.com
Received: by 10.231.48.9 with SMTP id p9ls2036117ibf.8.gmail; Fri, 22 Jun 2012
18:23:11 -0700 (PDT)
Received: by 10.50.186.228 with SMTP id fn4mr2771277igc.0.1340414591011;
Fri, 22 Jun 2012 18:23:11 -0700 (PDT)
Received: by 10.50.186.228 with SMTP id fn4mr2771276igc.0.1340414590996;
Fri, 22 Jun 2012 18:23:10 -0700 (PDT)
Return-Path: <mint...@easyesi.com>
Received: from mail-ob0-f182.google.com (mail-ob0-f182.google.com [209.85.214.182])
by gmr-mx.google.com with ESMTPS id bg4si46525igb.3.2012.06.22.18.23.10
(version=TLSv1/SSLv3 cipher=OTHER);
Fri, 22 Jun 2012 18:23:10 -0700 (PDT)
Received-SPF: neutral (google.com: 209.85.214.182 is neither permitted nor denied by best guess record for domain of mint...@easyesi.com) client-ip=209.85.214.182;
Authentication-Results: gmr-mx.google.com; spf=neutral (google.com: 209.85.214.182 is neither permitted nor denied by best guess record for domain of mint...@easyesi.com) smtp.mail=mint...@easyesi.com
Received: by mail-ob0-f182.google.com with SMTP id un3so2853771obb.41
for <google-gson@googlegroups.com>; Fri, 22 Jun 2012 18:23:10 -0700 (PDT)
d=google.com; s=20120113;
h=mime-version:x-originating-ip:in-reply-to:references:date
:message-id:subject:from:to:content-type:content-transfer-encoding
:x-gm-message-state;
bh=3rcVLzL0Q1feNwg8XbO5rIJ5crsvlAqoq7HxC52DUvE=;
b=JCgsRbO+B9qFa5I88mAVTFNJIrogIq4hbMSycF6Zo3KzjVk25FYuq/KBOlj9Sz+Ycm
Ha5oXVWNZYWgQSxjeiVPyCb7/pXExXuf6v+ibF5jVhpcjBCMdesKrRFRIDinI+f8OI22
b9mmbRZYIHIAmtJDvZN5ii+N2HX4fe9GlMfNx69G7T6eP5+bYxTBlDr7/zk1faOVk+Si
D+RRM19CO7sFGdJbGgAtiFHH3nqz2i/5p31WUmk/+ZHMreq663pfS4uhVXYbfQXi69gh
JeAKE/tzGzFeVcfUoPEtvnpwuO9mA2cl7MJ4VSalVJHSBvMOcPJyo+8tIwahUqq3uolo
SCHQ==
MIME-Version: 1.0
Received: by 10.182.131.2 with SMTP id oi2mr3956791obb.43.1340414590540; Fri,
22 Jun 2012 18:23:10 -0700 (PDT)
Received: by 10.76.11.5 with HTTP; Fri, 22 Jun 2012 18:23:10 -0700 (PDT)
X-Originating-IP: [76.103.244.140]
In-Reply-To: <55fa8f7e-694e-4d34-8361-c40c1e2e1...@y41g2000yqm.googlegroups.com>
References: <8d131b2a-2746-4226-88c1-8dc7447cc8d8@googlegroups.com>
<55fa8f7e-694e-4d34-8361-c40c1e2e1...@y41g2000yqm.googlegroups.com>
Date: Fri, 22 Jun 2012 18:23:10 -0700
Message-ID: <CAJW1onhW5JwNL9Jnjn+xe_Q3Ro=CvNg8nGBzy84-jSigGRR...@mail.gmail.com>
Subject: Re: [google-gson:1707]: Re: Default Value when deserialized?
From: Brandon Mintern <mint...@easyesi.com>
To: google-gson@googlegroups.com
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
X-Gm-Message-State: ALoCoQkWJHQVnBodlDDnPShds46jtnGwVH87Vc+yiG/PjVjASnztH7IrmQpCM/ETdU+0X03ZgQQt
If your class has a default constructor (even an empty one), your
first pattern (initializing the instances variables where they are
defined instead of in the constructor) should work fine. If your class
does not have a default constructor, I believe (I could be wrong) that
Gson manually tries to allocate the object, which would skip any
initialization at all. This will actually fail on some platforms (I
have seen posts to that effect about Android allocations failing).
So you should always make sure Gson-constructed classes have a default
constructor. It may be that putting the code you showed in the default
constructor worked not because you moved the code around, but because
you had a default constructor at all.
On Fri, Jun 22, 2012 at 11:07 AM, shaunpatterson
<shaunpatter...@gmail.com> wrote:
> It seems adding
>
> public Test() {
> =A0 this.string1 =3D "default value";
> =A0 this.list1 =3D new ArrayList<String>();
> }
>
> seems to do the trick. =A0Is this the proper way of doing this?
>
> --
> Shaun
>
>
> On Jun 21, 12:04=A0pm, shaunpatterson <shaunpatter...@gmail.com> wrote:
>> I have an object defined like this:
>>
>> public class Test {
>> =A0 =A0 public String string1 =3D "";
>> =A0 =A0 public List<String> list1 =3D new ArrayList<String>();
>>
>> }
>>
>> And this object is built from the following JSON:
>>
>> { "string1" : "This is a test" }
>>
>> When deserialized, list1 is null. =A0Is there a way to have Gson ignore
>> missing values? =A0I want list1 to be an empty ArrayList if the value is=
not
>> passed in the JSON.
>>
>> Thanks
>>
>> --
>> Shaun
>
> --
> You received this message because you are subscribed to the Google Groups=
"google-gson" group.
> To post to this group, send email to google-gson@googlegroups.com.
> To unsubscribe from this group, send email to google-gson+unsubscribe@goo=
glegroups.com.
> For more options, visit this group at http://groups.google.com/group/goog=
le-gson?hl=3Den.
>