-
Notifications
You must be signed in to change notification settings - Fork 5
Make HumanReadableByteCount serialize to JSON string instead of an object. #105
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make HumanReadableByteCount serialize to JSON string instead of an object. #105
Conversation
|
Note: This repo doesn't have a changelog policy, but the changelog status check is required to pass. |
|
|
||
| @Test | ||
| public void testParseByte() { | ||
| assetStringsEqualToBytes(10L, "10", "10b", "10 byte", "10 bytes"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice
| * @return a human-readable string representation of this byte string, not null | ||
| */ | ||
| @Override | ||
| @JsonValue |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Matches HumanReadableDuration which already applies @JsonValue to toString() 👍
|
One thing that's slightly interesting here is that this type doesn't do a byte-for-byte round-trip, because the jsoncreator does some normalizing. For example, I think I think this is fine, just kinda interesting. @JsonCreator
public static HumanReadableByteCount valueOf(String byteCount) {
String lower = byteCount.toLowerCase(Locale.ROOT).trim();
try {
Matcher matcher = BYTE_COUNT_PATTERN.matcher(lower);
Preconditions.checkArgument(matcher.matches(), "Invalid byte string: %s", byteCount);
long size = Long.parseLong(matcher.group(1));
String suffix = matcher.group(2);
if (suffix != null && !SUFFIXES.containsKey(suffix)) {
throw new IllegalArgumentException("Invalid byte string: " + byteCount + ". Wrong byte unit");
}
return new HumanReadableByteCount(size, suffix != null ? SUFFIXES.get(suffix) : ByteUnit.BYTE);
} catch (NumberFormatException e) {
String byteError = "Size must be specified as bytes (b), "
+ "kibibytes (k), mebibytes (m), gibibytes (g), tebibytes (t), or pebibytes(p). "
+ "E.g. 50b, 100k, or 250m.";
throw new NumberFormatException(byteError + "\n" + e.getMessage());
}
} |
No description provided.