- 
          
- 
                Notifications
    You must be signed in to change notification settings 
- Fork 354
Ref: Handle nested json configuration structures #4470
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Closed
      
      
            antonis
  wants to merge
  9
  commits into
  antonis/ref-convertToWritable
from
antonis/ref-RNSentryJsonUtils
  
      
      
   
      
    
  
     Closed
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            9 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      ab032e9
              
                Extract Json related functions to a separate utility class
              
              
                antonis bb42031
              
                Handle nested Json structures
              
              
                antonis 0783686
              
                Merge branch 'antonis/ref-convertToWritable' into antonis/ref-RNSentr…
              
              
                antonis 31c6299
              
                Adds test for deep nested json structures
              
              
                antonis ad460d5
              
                Merge branch 'antonis/ref-convertToWritable' into antonis/ref-RNSentr…
              
              
                antonis 5fb03ef
              
                Adds Javadoc and null annotations in json utils
              
              
                antonis 1b0b11d
              
                Merge branch 'antonis/ref-convertToWritable' into antonis/ref-RNSentr…
              
              
                antonis 63e8cbf
              
                Merge branch 'antonis/ref-convertToWritable' into antonis/ref-RNSentr…
              
              
                antonis f820b64
              
                Merge branch 'antonis/ref-convertToWritable' into antonis/ref-RNSentr…
              
              
                antonis File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
        
          
  
    
      
          
            102 changes: 102 additions & 0 deletions
          
          102 
        
  ...dTester/app/src/androidTest/java/io/sentry/rnsentryandroidtester/RNSentryJsonUtilsTest.kt
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| package io.sentry.rnsentryandroidtester | ||
|  | ||
| import android.content.Context | ||
| import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
| import androidx.test.platform.app.InstrumentationRegistry | ||
| import com.facebook.react.bridge.JavaOnlyMap | ||
| import com.facebook.soloader.SoLoader | ||
| import io.sentry.react.RNSentryJsonUtils | ||
| import org.json.JSONArray | ||
| import org.json.JSONObject | ||
| import org.junit.Assert.assertEquals | ||
| import org.junit.Assert.assertNotNull | ||
| import org.junit.Assert.assertTrue | ||
| import org.junit.Before | ||
| import org.junit.Test | ||
| import org.junit.runner.RunWith | ||
|  | ||
| @RunWith(AndroidJUnit4::class) | ||
| class RNSentryJsonUtilsTest { | ||
| @Before | ||
| fun setUp() { | ||
| val context: Context = InstrumentationRegistry.getInstrumentation().targetContext | ||
| SoLoader.init(context, false) | ||
| } | ||
|  | ||
| @Test | ||
| fun testJsonObjectToReadableMap() { | ||
| val json = | ||
| JSONObject().apply { | ||
| put("stringKey", "stringValue") | ||
| put("booleanKey", true) | ||
| put("intKey", 123) | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's also test test doubles/floats as those are common in Sentry configs. | ||
| } | ||
|  | ||
| val result = RNSentryJsonUtils.jsonObjectToReadableMap(json) | ||
|  | ||
| assertNotNull(result) | ||
| assertTrue(result is JavaOnlyMap) | ||
| assertEquals("stringValue", result?.getString("stringKey")) | ||
| assertEquals(true, result?.getBoolean("booleanKey")) | ||
| assertEquals(123, result?.getInt("intKey")) | ||
| } | ||
|  | ||
| @Test | ||
| fun testNestedJsonObjectToReadableMap() { | ||
| val json = | ||
| JSONObject().apply { | ||
| put("stringKey", "stringValue") | ||
| put("booleanKey", true) | ||
| put("intKey", 123) | ||
| put( | ||
| "nestedKey", | ||
| JSONObject().apply { | ||
| put("nestedStringKey", "nestedStringValue") | ||
| put("nestedBooleanKey", false) | ||
| put( | ||
| "deepNestedArrayKey", | ||
| JSONArray().apply { | ||
| put("deepNestedArrayValue") | ||
| }, | ||
| ) | ||
| }, | ||
| ) | ||
| put( | ||
| "arrayKey", | ||
| JSONArray().apply { | ||
| put("arrayStringValue") | ||
| put(789) | ||
| put( | ||
| JSONObject().apply { | ||
| put("deepNestedStringKey", "deepNestedStringValue") | ||
| put("deepNestedBooleanKey", false) | ||
| }, | ||
| ) | ||
| }, | ||
| ) | ||
| } | ||
|  | ||
| val result = RNSentryJsonUtils.jsonObjectToReadableMap(json) | ||
|  | ||
| assertNotNull(result) | ||
| assertTrue(result is JavaOnlyMap) | ||
| assertEquals("stringValue", result?.getString("stringKey")) | ||
| assertEquals(true, result?.getBoolean("booleanKey")) | ||
| assertEquals(123, result?.getInt("intKey")) | ||
| val nested = result?.getMap("nestedKey") | ||
| assertNotNull(nested) | ||
| assertEquals("nestedStringValue", nested?.getString("nestedStringKey")) | ||
| assertEquals(false, nested?.getBoolean("nestedBooleanKey")) | ||
| val deepNestedArray = nested?.getArray("deepNestedArrayKey") | ||
| assertNotNull(deepNestedArray) | ||
| assertEquals("deepNestedArrayValue", deepNestedArray?.getString(0)) | ||
| val array = result?.getArray("arrayKey") | ||
| assertNotNull(array) | ||
| assertEquals("arrayStringValue", array?.getString(0)) | ||
| assertEquals(789, array?.getInt(1)) | ||
| val deepNested = array?.getMap(2) | ||
| assertNotNull(deepNested) | ||
| assertEquals("deepNestedStringValue", deepNested?.getString("deepNestedStringKey")) | ||
| assertEquals(false, deepNested?.getBoolean("deepNestedBooleanKey")) | ||
| } | ||
| } | ||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
        
          
  
    
      
          
            110 changes: 110 additions & 0 deletions
          
          110 
        
  packages/core/android/src/main/java/io/sentry/react/RNSentryJsonUtils.java
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| package io.sentry.react; | ||
|  | ||
| import android.content.Context; | ||
| import com.facebook.react.bridge.ReadableMap; | ||
| import com.facebook.react.bridge.WritableMap; | ||
| import io.sentry.ILogger; | ||
| import io.sentry.SentryLevel; | ||
| import java.io.BufferedReader; | ||
| import java.io.InputStream; | ||
| import java.io.InputStreamReader; | ||
| import java.util.ArrayList; | ||
| import java.util.HashMap; | ||
| import java.util.Iterator; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import org.jetbrains.annotations.NotNull; | ||
| import org.jetbrains.annotations.Nullable; | ||
| import org.json.JSONArray; | ||
| import org.json.JSONException; | ||
| import org.json.JSONObject; | ||
|  | ||
| public final class RNSentryJsonUtils { | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's not expose this outside of the package. | ||
| private RNSentryJsonUtils() { | ||
| throw new AssertionError("Utility class should not be instantiated"); | ||
| } | ||
|  | ||
| /** | ||
| * Read the configuration file in the Android assets folder and return the options as a | ||
| * JSONObject. | ||
| * | ||
| * @param context Android Context | ||
| * @param fileName configuration file name | ||
| * @param logger Sentry logger | ||
| * @return JSONObject with the configuration options | ||
| */ | ||
| public static @Nullable JSONObject getOptionsFromConfigurationFile( | ||
| @NotNull Context context, @NotNull String fileName, @NotNull ILogger logger) { | ||
| try (InputStream inputStream = context.getAssets().open(fileName); | ||
| BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) { | ||
|  | ||
| StringBuilder stringBuilder = new StringBuilder(); | ||
| String line; | ||
| while ((line = reader.readLine()) != null) { | ||
| stringBuilder.append(line); | ||
| } | ||
| String configFileContent = stringBuilder.toString(); | ||
| return new JSONObject(configFileContent); | ||
|  | ||
| } catch (Exception e) { | ||
| logger.log( | ||
| SentryLevel.ERROR, | ||
| "Failed to read configuration file. Please make sure " | ||
| + fileName | ||
| + " exists in the root of your project.", | ||
| e); | ||
| return null; | ||
| } | ||
| } | ||
|  | ||
| private static @NotNull Map<String, Object> jsonObjectToMap(@NotNull JSONObject jsonObject) { | ||
| Map<String, Object> map = new HashMap<>(); | ||
| Iterator<String> keys = jsonObject.keys(); | ||
| while (keys.hasNext()) { | ||
| String key = keys.next(); | ||
| Object value = null; | ||
| try { | ||
| value = jsonObject.get(key); | ||
| } catch (JSONException e) { | ||
| throw new RuntimeException(e); | ||
| } | ||
| map.put(key, convertValue(value)); | ||
| } | ||
| return map; | ||
| } | ||
|  | ||
| private static @NotNull List<Object> jsonArrayToList(@NotNull JSONArray jsonArray) { | ||
| List<Object> list = new ArrayList<>(); | ||
|  | ||
| for (int i = 0; i < jsonArray.length(); i++) { | ||
| Object value = jsonArray.opt(i); | ||
| list.add(convertValue(value)); | ||
| } | ||
|  | ||
| return list; | ||
| } | ||
|  | ||
| private static @Nullable Object convertValue(@Nullable Object value) { | ||
| if (value instanceof JSONObject) { | ||
| return jsonObjectToMap((JSONObject) value); | ||
| } else if (value instanceof JSONArray) { | ||
| return jsonArrayToList((JSONArray) value); | ||
| } else { | ||
| return value; // Primitive type or null | ||
| } | ||
| } | ||
|  | ||
| /** | ||
| * Convert a JSONObject to a ReadableMap | ||
| * | ||
| * @param jsonObject JSONObject to convert | ||
| * @return ReadableMap with the same data as the JSONObject | ||
| */ | ||
| public static @Nullable ReadableMap jsonObjectToReadableMap(@Nullable JSONObject jsonObject) { | ||
| if (jsonObject == null) { | ||
| return null; | ||
| } | ||
| Map<String, Object> map = jsonObjectToMap(jsonObject); | ||
| return (WritableMap) RNSentryMapConverter.convertToJavaWritable(map); | ||
| } | ||
| } | ||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should not be needed. Let's remove it, to know 100% that these conversions can run before the RN initializes.