elvishew/xLog

XLog.json(JsonString); 当JsonString异常时会造成崩溃

Closed this issue · 2 comments

建议检测一下,防止崩溃!

@maning0303 Thank you for your advice. After reviewing the code, I think we should remain the Exception to notice the library user that something went wrong, for example the back-end returning a wrong JSON, when still in early development.

If you don't like the Exception or crashing when the JSON string is wrong, you can specify your own JsonFormatter when initializing xLog. Something like below.

        final JsonFormatter defaultJsonFormatter = new DefaultJsonFormatter();
        final JsonFormatter safeJsonFormatter = new JsonFormatter() {
            @Override
            public String format(String data) {
                try {
                    return defaultJsonFormatter.format(data);
                } catch (Exception e) {
                    return "{Something went wrong}";
                }
            }
        };
        XLog.init(new LogConfiguration.Builder()
                .jsonFormatter(safeJsonFormatter)
                ...
                .build());

@elvishew Thank you!