FrankChen021 / bithon

An observability platform mainly for Java
Apache License 2.0
14 stars 4 forks source link

Use uppercase letters for environment variable naming. #809

Open kai8406 opened 5 days ago

kai8406 commented 5 days ago

Use uppercase letters for environment variable naming.

808

FrankChen021 commented 4 days ago

jackson supports to deserialize objects in case insensitive way. Maybe this is the simplest way. We can configure the ObjectMapper inside the org.bithon.agent.configuration.Binder.

Here's the sample code

//
// Illustration of deserialization from uppercase 'Age' to lower case property 'age'
//
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;

public class Main {
    public static void main(String[] args) throws Exception {
        ObjectMapper mapper = new ObjectMapper();
        // Enable case-insensitive property matching
        mapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);

        String json = "{\"name\": \"John\", \"Age\": 30}";

        Person person = mapper.readValue(json, Person.class);
        System.out.println(person);
    }
}

class Person {
    private String name;
    private int age;

    // getters and setters
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Person{name='" + name + "', age=" + age + '}';
    }
}
//
// Illustration of same properties in different cases
//
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;

public class Main {
    public static void main(String[] args) throws Exception {
        ObjectMapper mapper = new ObjectMapper();
        // Enable case-insensitive property matching
        mapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);

        String json = "{\"name\": \"John\", \"Name\": \"Doe\"}";

        Person person = mapper.readValue(json, Person.class);
        System.out.println(person);
    }
}

class Person {
    private String name;

    // getters and setters
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Person{name='" + name + "'}";
    }
}