-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
42 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,16 @@ | ||
package com.epam.aidial.core.config; | ||
|
||
import com.epam.aidial.core.util.DoubleStringDeserializer; | ||
import com.fasterxml.jackson.databind.annotation.JsonDeserialize; | ||
import lombok.Data; | ||
|
||
@Data | ||
public class Pricing { | ||
private String unit; | ||
private Double prompt; | ||
private Double completion; | ||
|
||
@JsonDeserialize(using = DoubleStringDeserializer.class) | ||
private String prompt; | ||
|
||
@JsonDeserialize(using = DoubleStringDeserializer.class) | ||
private String completion; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
src/main/java/com/epam/aidial/core/util/DoubleStringDeserializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.epam.aidial.core.util; | ||
|
||
import com.fasterxml.jackson.core.JsonParser; | ||
import com.fasterxml.jackson.core.JsonToken; | ||
import com.fasterxml.jackson.databind.DeserializationContext; | ||
import com.fasterxml.jackson.databind.JsonDeserializer; | ||
import com.fasterxml.jackson.databind.exc.InvalidFormatException; | ||
|
||
import java.io.IOException; | ||
|
||
public class DoubleStringDeserializer extends JsonDeserializer<String> { | ||
|
||
@Override | ||
public String deserialize(JsonParser p, DeserializationContext ctx) throws IOException { | ||
if (p.getCurrentToken() != JsonToken.VALUE_STRING) { | ||
throw InvalidFormatException.from(p, "Expected a JSON string", p.getText(), String.class); | ||
} | ||
|
||
String value = p.getValueAsString(); | ||
try { | ||
Double.parseDouble(value); | ||
return value; | ||
} catch (NumberFormatException e) { | ||
throw InvalidFormatException.from(p, "Expected a JSON string with a valid double", value, String.class); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters