下面列出了怎么用io.jsonwebtoken.io.Encoders的API类实例代码及写法,或者点击链接到github查看源代码。
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.");
}
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);
}
}
@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);
}
public static String encodeKeyBase64(Key key) {
return Encoders.BASE64.encode(key.getEncoded());
}
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);
}
@Override
public String encode(byte[] data) {
return Encoders.BASE64.encode(data);
}
@Deprecated
public DefaultJwtSigner(SignatureAlgorithm alg, Key key) {
this(DefaultSignerFactory.INSTANCE, alg, key, Encoders.BASE64URL);
}
@Deprecated
public DefaultJwtSigner(SignerFactory factory, SignatureAlgorithm alg, Key key) {
this(factory, alg, key, Encoders.BASE64URL);
}
@Override
public String encode(byte[] data) {
return Encoders.BASE64URL.encode(data);
}
public String encode(byte[] data) {
return Encoders.BASE64.encode(data);
}