All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.backblaze.b2.json.B2JsonCharacterHandler Maven / Gradle / Ivy

Go to download

The core logic for B2 SDK for Java. Does not include any implementations of B2WebApiClient.

There is a newer version: 6.3.0
Show newest version
/*
 * Copyright 2017, Backblaze Inc. All Rights Reserved.
 * License https://www.backblaze.com/using_b2_code.html
 */

package com.backblaze.b2.json;

import java.io.IOException;

/**
 * (De)serializes Character objects.
 */
public class B2JsonCharacterHandler implements B2JsonTypeHandler {

    private final boolean isPrimitive;

    public B2JsonCharacterHandler(boolean isPrimitive) {
        this.isPrimitive = isPrimitive;
    }

    public Class getHandledClass() {
        return Character.class;
    }

    public void serialize(Character obj, B2JsonWriter out) throws IOException {
        out.writeText(Integer.toString((int) obj.charValue()));
    }

    public Character deserialize(B2JsonReader in, int options) throws B2JsonException, IOException {
        String str = in.readNumberAsString();
        try {
            int value = Integer.valueOf(str);
            if (value < 0 || 0xFFFF < value) {
                throw new B2JsonException("char value out of range: " + str);
            }
            return (char) value;
        }
        catch (NumberFormatException e) {
            throw new B2JsonException("bad character: " + str);
        }
    }

    public Character deserializeUrlParam(String urlValue) throws B2JsonException {
        try {
            int value = Integer.valueOf(urlValue);
            if (value < 0 || 0xFFFF < value) {
                throw new B2JsonException("char value out of range: " + urlValue);
            }
            return (char) value;
        }
        catch (NumberFormatException e) {
            throw new B2JsonException("bad character: " + urlValue);
        }
    }

    public Character defaultValueForOptional() {
        if (isPrimitive) {
            return 0;
        }
        else {
            return null;
        }
    }

    public boolean isStringInJson() {
        return false;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy