joerghoh commented on code in PR #22:
URL: 
https://github.com/apache/sling-org-apache-sling-i18n/pull/22#discussion_r3146420715


##########
src/main/java/org/apache/sling/i18n/impl/JcrResourceBundle.java:
##########
@@ -245,62 +251,29 @@ private Map<String, Object> loadFully(
     private void loadJsonDictionary(Resource resource, final Map<String, 
Object> targetDictionary) {
         log.info("Loading json dictionary: {}", resource.getPath());
 
-        // use streaming parser (we don't need the dict in memory twice)
-        JsonParser parser = new JsonParser(new JsonHandler() {
-
-            private String key;
-
-            @Override
-            public void key(String key) throws IOException {
-                this.key = key;
-            }
-
-            @Override
-            public void value(String value) throws IOException {
-                targetDictionary.put(key, value);
-            }
-
-            @Override
-            public void object() throws IOException {}
-
-            @Override
-            public void endObject() throws IOException {}
-
-            @Override
-            public void array() throws IOException {}
-
-            @Override
-            public void endArray() throws IOException {}
-
-            @Override
-            public void value(boolean value) throws IOException {}
-
-            @Override
-            public void value(long value) throws IOException {}
-
-            @Override
-            public void value(double value) throws IOException {}
-        });
-
         final InputStream stream = resource.adaptTo(InputStream.class);
         if (stream != null) {
-            String encoding = "utf-8";
-            final ResourceMetadata metadata = resource.getResourceMetadata();
-            if (metadata.getCharacterEncoding() != null) {
-                encoding = metadata.getCharacterEncoding();
-            }
-
-            try {
-
-                parser.parse(stream, encoding);
+            try (InputStream input = stream) {
+                Charset charset = StandardCharsets.UTF_8;
+                final ResourceMetadata metadata = 
resource.getResourceMetadata();
+                if (metadata.getCharacterEncoding() != null) {
+                    charset = Charset.forName(metadata.getCharacterEncoding());
+                }
 
-            } catch (IOException e) {
-                log.warn("Could not parse i18n json dictionary {}: {}", 
resource.getPath(), e.getMessage());
-            } finally {
-                try {
-                    stream.close();
-                } catch (IOException ignore) {
+                try (InputStreamReader reader = new InputStreamReader(input, 
charset);
+                        JsonParser parser = 
JsonProvider.provider().createParser(reader)) {
+                    String key = null;
+                    while (parser.hasNext()) {
+                        final Event event = parser.next();
+                        if (event == Event.KEY_NAME) {
+                            key = parser.getString();
+                        } else if (event == Event.VALUE_STRING && key != null) 
{
+                            targetDictionary.put(key, parser.getString());
+                        }

Review Comment:
   I would log it on DEBUG level (WARN does not make sense for me, as the 
particular format here is straight forward, and it should be easy to fix.)



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to