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

com.backblaze.b2.json.B2JsonBooleanHandler 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 Boolean objects.
 */
public class B2JsonBooleanHandler implements B2JsonTypeHandler {

    private final boolean isPrimitive;

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

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

    public void serialize(Boolean obj, B2JsonWriter out) throws IOException {
        out.writeText(obj.toString());
    }

    public Boolean deserialize(B2JsonReader in, int options) throws B2JsonException, IOException {
        char c = in.peekNextNotWhitespaceChar();
        if (c == 'f') {
            in.readFalse();
            return false;
        }
        else if (c == 't') {
            in.readTrue();
            return true;
        }
        else {
            throw new B2JsonException("expected boolean, but next char was " + c);
        }
    }

    public Boolean deserializeUrlParam(String urlValue) throws B2JsonException {
        if ("true".equals(urlValue)) {
            return true;
        }
        else if ("false".equals(urlValue)) {
            return false;
        }
        else {
            throw new B2JsonException("expected boolean, but value was " + urlValue);
        }
    }

    public Boolean defaultValueForOptional() {
        if (isPrimitive) {
            return false;
        }
        else {
            return null;
        }
    }

    public boolean isStringInJson() {
        return false;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy