我有一个带有created
Date属性的Contact类。我试图做以下事情:
Contact received = gson.fromJson(contactJson, Contact.class);
但是我得到了例外:
com.google.gson.JsonSyntaxException: 1433444958703
at com.google.gson.internal.bind.DateTypeAdapter.deserializeToDate(DateTypeAdapter.java:81)
at com.google.gson.internal.bind.DateTypeAdapter.read(DateTypeAdapter.java:66)
已经发布了类似的解决方案: "Unparseable date: 1302828677828" trying to deserialize with Gson a millisecond-format date received from server
然而它对我不起作用(不会编译)。
Gson版本2.3.1
思想?
TIA, - Ole
答案 0 :(得分:1)
在这里找到一个类似的解决方案(Gson: JsonSyntaxException on date),并稍微调整一下:
@implementation RDView
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
_nodes = [NSMutableArray new];
for (int i = 0; i<10; i++) {
NodeView *aNode = [NodeView new];
CGFloat hue = arc4random_uniform(1000)/1000.0;
aNode.backgroundColor = [UIColor colorWithHue:hue saturation:1 brightness:1 alpha:.7];
CGFloat xVal = arc4random_uniform(300);
CGFloat yVal = arc4random_uniform(400) + 50;
aNode.frame = CGRectMake(xVal, yVal, 20, 20);
[self addSubview:aNode];
UIPanGestureRecognizer *panner = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
[aNode addGestureRecognizer:panner];
[_nodes addObject:aNode];
}
}
return self;
}
-(void)awakeFromNib {
for (NodeView *aNode in self.nodes) {
[aNode addObserver:self forKeyPath:@"center" options:NSKeyValueObservingOptionNew context:nil];
}
}
-(void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if ([keyPath isEqualToString:@"center"] ) [self setNeedsDisplay];
}
- (void)drawRect:(CGRect)rect {
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:[self.nodes[0] center]];
for (int i = 1; i<self.nodes.count; i++) {
CGFloat dashes[] = {0, path.lineWidth * 2};
[path setLineDash:dashes count:2 phase:0];
path.lineCapStyle = kCGLineCapRound;
[path addLineToPoint:[self.nodes[i] center]];
}
[path closePath];
[path stroke];
}
- (IBAction)handlePan:(UIPanGestureRecognizer *)recognizer {
CGPoint translation = [recognizer translationInView:self];
recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x, recognizer.view.center.y + translation.y);
[recognizer setTranslation:CGPointMake(0, 0) inView:self];
}
这很有效。如果这是GSON中的默认值,那将会很棒。)。
答案 1 :(得分:0)
您可以编写一个简单的Type Adapter并使用GsonBuilder实例注册它,或者在Contact类本身中使用@JsonAdapter注释。请注意,您应该将日期序列化为长字符串,因为long不是有效的JSON类型,可能会被修剪为int(在大多数日期肯定会失败)。
以下是适配器的外观:
public class LongDateTypeAdapter extends TypeAdapter<Date> {
@Override public void write(JsonWriter out, Date value) throws IOException {
if (value == null) {
out.nullValue();
} else {
out.value(String.valueOf(value.getTime()));
}
}
@Override public Date read(JsonReader in) throws IOException {
switch (in.peek()) {
case NULL: return null;
case STRING:
try {
return new Date(Long.parseLong(in.nextString()));
} catch (NumberFormatException e) {
throw new JsonSyntaxException(e);
}
default: throw new JsonSyntaxException("invalid date" + in.getPath());
}
}
}
您可以在以下位置查看源代码: https://github.com/google-gson/typeadapters/blob/master/common/src/main/java/LongDateTypeAdapter.java
以下是说明如何使用它的测试: https://github.com/google-gson/typeadapters/blob/master/common/src/test/java/LongDateTypeAdapterTest.java