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

trydofor / professional-wings / #131

16 Jan 2025 02:20PM UTC coverage: 63.526% (+0.03%) from 63.495%
#131

push

trydofor
⬆️ 💥 up deps, boot 3.3, mirana new i18n #309

30 of 68 new or added lines in 28 files covered. (44.12%)

36 existing lines in 3 files now uncovered.

12920 of 20338 relevant lines covered (63.53%)

0.64 hits per line

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

92.11
/wings/slardar/src/main/java/pro/fessional/wings/slardar/jackson/I18nStringSerializer.java
1
package pro.fessional.wings.slardar.jackson;
2

3
import com.fasterxml.jackson.core.JsonGenerator;
4
import com.fasterxml.jackson.databind.BeanProperty;
5
import com.fasterxml.jackson.databind.JsonSerializer;
6
import com.fasterxml.jackson.databind.SerializerProvider;
7
import com.fasterxml.jackson.databind.ser.ContextualSerializer;
8
import org.springframework.context.MessageSource;
9
import pro.fessional.mirana.i18n.I18nString;
10
import pro.fessional.wings.slardar.autodto.AutoI18nString;
11
import pro.fessional.wings.slardar.context.LocaleZoneIdUtil;
12

13
import java.io.IOException;
14
import java.util.Locale;
15
import java.util.concurrent.atomic.AtomicReference;
16

17
/**
18
 * @author trydofor
19
 * @since 2019-09-19
20
 */
21
public class I18nStringSerializer extends JsonSerializer<Object> implements ContextualSerializer {
22

23
    private final AtomicReference<I18nStringSerializer> oppositeOne = new AtomicReference<>();
1✔
24
    private final MessageSource messageSource;
25
    private final boolean enabled;
26

27
    public I18nStringSerializer(MessageSource messageSource, boolean enabled) {
1✔
28
        this.messageSource = messageSource;
1✔
29
        this.enabled = enabled;
1✔
30
    }
1✔
31

32
    @Override
33
    public void serialize(Object value, JsonGenerator generator, SerializerProvider provider) throws IOException {
34

35
        if (!(value instanceof CharSequence) && !(value instanceof I18nString)) {
1✔
36
            provider.defaultSerializeValue(value, generator);
×
37
            return;
×
38
        }
39

40
        if (value instanceof CharSequence) {
1✔
41
            String text = value.toString();
1✔
42
            if (enabled) {
1✔
43
                Locale locale = LocaleZoneIdUtil.LocaleNonnull();
1✔
44
                text = messageSource.getMessage(text, new Object[]{}, locale);
1✔
45
            }
46
            generator.writeString(text);
1✔
47
        }
1✔
48
        else { // value instanceof I18nString
49
            I18nString i18n = (I18nString) value;
1✔
50
            if (enabled) {
1✔
51
                Locale locale = LocaleZoneIdUtil.LocaleNonnull();
1✔
52
                String text = i18n.toString(locale, messageSource::getMessage);
1✔
53
                if (text == null || text.equalsIgnoreCase(i18n.getI18nCode())) {
1✔
NEW
54
                    text = i18n.toString();
×
55
                }
56
                generator.writeString(text);
1✔
57
            }
1✔
58
            else {
59
                generator.writeStartObject();
1✔
60
                generator.writeStringField("code", i18n.getI18nCode());
1✔
61
                generator.writeStringField("hint", i18n.getI18nHint());
1✔
62
                generator.writeFieldName("args");
1✔
63
                generator.writeObject(i18n.getI18nArgs());
1✔
64
                generator.writeEndObject();
1✔
65
            }
66
        }
67
    }
1✔
68

69
    @Override
70
    public JsonSerializer<?> createContextual(SerializerProvider provider, BeanProperty property) {
71
        if (property == null) return this;
1✔
72
        AutoI18nString ann = property.getAnnotation(AutoI18nString.class);
1✔
73
        if (ann == null || ann.value() == enabled) return this;
1✔
74

75
        I18nStringSerializer that = oppositeOne.get();
1✔
76
        // No sync required, no impact on results
77
        if (that == null) {
1✔
78
            that = new I18nStringSerializer(messageSource, !enabled);
1✔
79
            oppositeOne.set(that);
1✔
80
        }
81
        return that;
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

© 2026 Coveralls, Inc