2 * Copyright (C) 2011 Google Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com
.google
.gson
.internal
;
18 import java
.io
.ObjectStreamException
;
19 import java
.math
.BigDecimal
;
22 * This class holds a number value that is lazily converted to a specific number type
24 * @author Inderjeet Singh
26 public final class LazilyParsedNumber
extends Number
{
27 private final String value
;
29 public LazilyParsedNumber(String value
) {
34 public int intValue() {
36 return Integer
.parseInt(value
);
37 } catch (NumberFormatException e
) {
39 return (int) Long
.parseLong(value
);
40 } catch (NumberFormatException nfe
) {
41 return new BigDecimal(value
).intValue();
47 public long longValue() {
49 return Long
.parseLong(value
);
50 } catch (NumberFormatException e
) {
51 return new BigDecimal(value
).longValue();
56 public float floatValue() {
57 return Float
.parseFloat(value
);
61 public double doubleValue() {
62 return Double
.parseDouble(value
);
66 public String
toString() {
71 * If somebody is unlucky enough to have to serialize one of these, serialize
72 * it as a BigDecimal so that they won't need Gson on the other side to
75 private Object
writeReplace() throws ObjectStreamException
{
76 return new BigDecimal(value
);