• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

optimizely / java-sdk / 2001

pending completion
2001

push

travis-ci-com

jaeopt
patches cherry-picked

4889 of 5384 relevant lines covered (90.81%)

0.91 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

50.0
/core-api/src/main/java/com/optimizely/ab/event/internal/serializer/DefaultJsonSerializer.java
1
/**
2
 *
3
 *    Copyright 2016-2017, 2019, Optimizely and contributors
4
 *
5
 *    Licensed under the Apache License, Version 2.0 (the "License");
6
 *    you may not use this file except in compliance with the License.
7
 *    You may obtain a copy of the License at
8
 *
9
 *        http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 *    Unless required by applicable law or agreed to in writing, software
12
 *    distributed under the License is distributed on an "AS IS" BASIS,
13
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 *    See the License for the specific language governing permissions and
15
 *    limitations under the License.
16
 */
17
package com.optimizely.ab.event.internal.serializer;
18

19
import org.slf4j.Logger;
20
import org.slf4j.LoggerFactory;
21

22
import com.optimizely.ab.config.parser.MissingJsonParserException;
23

24
import javax.annotation.Nonnull;
25

26
/**
27
 * Factory for generating {@link Serializer} instances, based on the json library available on the classpath.
28
 */
29
public final class DefaultJsonSerializer {
30

31
    private static final Logger logger = LoggerFactory.getLogger(DefaultJsonSerializer.class);
1✔
32

33
    private DefaultJsonSerializer() {
34
    }
35

36
    public static Serializer getInstance() {
37
        return LazyHolder.INSTANCE;
1✔
38
    }
39

40
    //======== Helper methods ========//
41

42
    /**
43
     * Creates and returns a {@link Serializer} using a json library available on the classpath.
44
     *
45
     * @return the created serializer
46
     * @throws MissingJsonParserException if there are no supported json libraries available on the classpath
47
     */
48
    private static @Nonnull
49
    Serializer create() {
50
        Serializer serializer;
51

52
        if (isPresent("com.fasterxml.jackson.databind.ObjectMapper")) {
1✔
53
            serializer = new JacksonSerializer();
1✔
54
        } else if (isPresent("com.google.gson.Gson")) {
×
55
            serializer = new GsonSerializer();
×
56
        } else if (isPresent("org.json.simple.JSONObject")) {
×
57
            serializer = new JsonSimpleSerializer();
×
58
        } else if (isPresent("org.json.JSONObject")) {
×
59
            serializer = new JsonSerializer();
×
60
        } else {
61
            throw new MissingJsonParserException("unable to locate a JSON parser. "
×
62
                + "Please see <link> for more information");
63
        }
64

65
        logger.debug("using json serializer: {}", serializer.getClass().getSimpleName());
1✔
66
        return serializer;
1✔
67
    }
68

69
    private static boolean isPresent(@Nonnull String className) {
70
        try {
71
            Class.forName(className);
1✔
72
            return true;
1✔
73
        } catch (ClassNotFoundException e) {
×
74
            return false;
×
75
        }
76
    }
77

78
    //======== Lazy-init Holder ========//
79

80
    private static class LazyHolder {
81
        private static final Serializer INSTANCE = create();
1✔
82
    }
83
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc