io.jsonwebtoken.io.Encoders Java Examples

The following examples show how to use io.jsonwebtoken.io.Encoders. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JjwtSerializer.java    From ditto with Eclipse Public License 2.0 5 votes vote down vote up
private static JsonValue toJson(final Object input) {

        if (input == null) {
            return JsonFactory.nullLiteral();
        } else if (input instanceof Boolean) {
            return JsonFactory.newValue((boolean) input);
        } else if (input instanceof Byte || input instanceof Short || input instanceof Integer) {
            return JsonFactory.newValue((int) input);
        } else if (input instanceof Long) {
            return JsonFactory.newValue((long) input);
        } else if (input instanceof Float) {
            return JsonFactory.newValue((float) input);
        } else if (input instanceof Double) {
            return JsonFactory.newValue((double) input);
        } else if (input instanceof Character || input instanceof String || input instanceof Enum) {
            return JsonFactory.newValue(input.toString());
        } else if (input instanceof Calendar) {
            return JsonFactory.newValue(DateFormats.formatIso8601(((Calendar) input).getTime()));
        } else if (input instanceof Date) {
            return JsonFactory.newValue(DateFormats.formatIso8601((Date) input));
        } else if (input instanceof byte[]) {
            return JsonFactory.newValue(Encoders.BASE64.encode((byte[]) input));
        } else if (input instanceof char[]) {
            return JsonFactory.newValue(new String((char[]) input));
        } else if (input instanceof Map) {
            return toJsonObject((Map<?, ?>) input);
        } else if (input instanceof Collection) {
            return toJsonArray((Collection<?>) input);
        } else if (Objects.isArray(input)) {
            return toJsonArray(Collections.arrayToList(input));
        }

        throw new SerializationException("Unable to serialize object of type " + input.getClass().getName() +
                " to JSON using known heuristics.");
    }
 
Example #2
Source File: TokensCliUtils.java    From pulsar with Apache License 2.0 5 votes vote down vote up
public void run() throws IOException {
    SecretKey secretKey = AuthTokenUtils.createSecretKey(algorithm);
    byte[] encoded = secretKey.getEncoded();

    if (base64) {
        encoded = Encoders.BASE64.encode(encoded).getBytes();
    }

    if (outputFile != null) {
        Files.write(Paths.get(outputFile), encoded);
    } else {
        System.out.write(encoded);
    }
}
 
Example #3
Source File: GsonSerializer.java    From jjwt with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("WeakerAccess") //for testing
protected byte[] writeValueAsBytes(T t) {
    Object o;
    if (t instanceof byte[]) {
        o = Encoders.BASE64.encode((byte[]) t);
    } else if (t instanceof char[]) {
        o = new String((char[]) t);
    } else {
        o = t;
    }
    return this.gson.toJson(o).getBytes(Strings.UTF_8);
}
 
Example #4
Source File: AuthTokenUtils.java    From pulsar with Apache License 2.0 4 votes vote down vote up
public static String encodeKeyBase64(Key key) {
    return Encoders.BASE64.encode(key.getEncoded());
}
 
Example #5
Source File: OrgJsonSerializer.java    From jjwt with Apache License 2.0 4 votes vote down vote up
private Object toJSONInstance(Object object) {

        if (object == null) {
            return JSONObject.NULL;
        }

        if (object instanceof JSONObject || object instanceof JSONArray
            || JSONObject.NULL.equals(object) || isJSONString(object)
            || object instanceof Byte || object instanceof Character
            || object instanceof Short || object instanceof Integer
            || object instanceof Long || object instanceof Boolean
            || object instanceof Float || object instanceof Double
            || object instanceof String || object instanceof BigInteger
            || object instanceof BigDecimal || object instanceof Enum) {
            return object;
        }

        if (object instanceof Calendar) {
            object = ((Calendar) object).getTime(); //sets object to date, will be converted in next if-statement:
        }

        if (object instanceof Date) {
            Date date = (Date) object;
            return DateFormats.formatIso8601(date);
        }

        if (object instanceof byte[]) {
            return Encoders.BASE64.encode((byte[]) object);
        }

        if (object instanceof char[]) {
            return new String((char[]) object);
        }

        if (object instanceof Map) {
            Map<?, ?> map = (Map<?, ?>) object;
            return toJSONObject(map);
        }
        if (object instanceof Collection) {
            Collection<?> coll = (Collection<?>) object;
            return toJSONArray(coll);
        }
        if (Objects.isArray(object)) {
            Collection c = Collections.arrayToList(object);
            return toJSONArray(c);
        }

        //not an immediately JSON-compatible object and probably a JavaBean (or similar).  We can't convert that
        //directly without using a marshaller of some sort:
        String msg = "Unable to serialize object of type " + object.getClass().getName() + " to JSON using known heuristics.";
        throw new SerializationException(msg);
    }
 
Example #6
Source File: AndroidBase64Codec.java    From jjwt with Apache License 2.0 4 votes vote down vote up
@Override
public String encode(byte[] data) {
    return Encoders.BASE64.encode(data);
}
 
Example #7
Source File: DefaultJwtSigner.java    From jjwt with Apache License 2.0 4 votes vote down vote up
@Deprecated
public DefaultJwtSigner(SignatureAlgorithm alg, Key key) {
    this(DefaultSignerFactory.INSTANCE, alg, key, Encoders.BASE64URL);
}
 
Example #8
Source File: DefaultJwtSigner.java    From jjwt with Apache License 2.0 4 votes vote down vote up
@Deprecated
public DefaultJwtSigner(SignerFactory factory, SignatureAlgorithm alg, Key key) {
    this(factory, alg, key, Encoders.BASE64URL);
}
 
Example #9
Source File: Base64UrlCodec.java    From jjwt with Apache License 2.0 4 votes vote down vote up
@Override
public String encode(byte[] data) {
    return Encoders.BASE64URL.encode(data);
}
 
Example #10
Source File: Base64Codec.java    From jjwt with Apache License 2.0 4 votes vote down vote up
public String encode(byte[] data) {
    return Encoders.BASE64.encode(data);
}