source: issuevalue/src/main/java/geniusweb/issuevalue/Value.java@ 40

Last change on this file since 40 was 40, checked in by bart, 3 years ago

LEARN algorithm now also available. Fixed small issues.

File size: 1.6 KB
Line 
1package geniusweb.issuevalue;
2
3import java.io.IOException;
4
5import com.fasterxml.jackson.core.JsonParser;
6import com.fasterxml.jackson.core.JsonProcessingException;
7import com.fasterxml.jackson.databind.DeserializationContext;
8import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
9import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
10
11/**
12 * A possible value for an Issue. Must be immutable and thread safe. Supported
13 * values are {@link NumberValue} and {@link DiscreteValue}. All values are just
14 * strings, a custom deserializer is used to determine which type it is.
15 *
16 * Value must be de-serializable all by itself, because it can occur plain in a
17 * Bid eg Bid["fte":0.8]
18 */
19@JsonDeserialize(using = ValueDeserializer.class)
20public interface Value {
21
22}
23
24/**
25 * Deserializes a Value. Special deserializer is needed because by default
26 * jackson deserializes numbers as double and thus cause rounding errors.
27 */
28@SuppressWarnings("serial")
29class ValueDeserializer extends StdDeserializer<Value> {
30
31 public ValueDeserializer() {
32 this(null);
33 }
34
35 public ValueDeserializer(Class<?> vc) {
36 super(vc);
37 }
38
39 @Override
40 public Value deserialize(JsonParser jp, DeserializationContext ctxt)
41 throws IOException, JsonProcessingException {
42 String text = jp.getText();
43 switch (jp.currentToken()) {
44 case VALUE_NUMBER_FLOAT:
45 case VALUE_NUMBER_INT:
46 return new NumberValue(text);
47 case VALUE_STRING:
48 return new DiscreteValue(text);
49 default:
50 throw new IOException(
51 "Expected number of double quoted string but found " + text
52 + " of type " + jp.currentToken());
53 }
54
55 }
56
57}
Note: See TracBrowser for help on using the repository browser.