Skip to content

Commit

Permalink
[JSON] fix: convert only styled strings
Browse files Browse the repository at this point in the history
  • Loading branch information
REAndroid committed May 2, 2023
1 parent 3289d33 commit e8fff62
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 22 deletions.
16 changes: 8 additions & 8 deletions src/main/java/com/reandroid/apk/SingleJsonTableInputSource.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/*
* Copyright (C) 2022 github.com/REAndroid
*
* Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -52,22 +52,22 @@ public long getCrc() throws IOException {
return outputStream.getCrcValue();
}
public TableBlock getTableBlock() throws IOException{
if(mCache!=null){
if(mCache != null){
return mCache;
}
logMessage("Building resources table: "+inputSource.getAlias());
logMessage("Building resources table: " + inputSource.getAlias());
TableBlock tableBlock=newInstance();
InputStream inputStream=inputSource.openStream();
InputStream inputStream = inputSource.openStream();
try{
StringPoolBuilder poolBuilder=new StringPoolBuilder();
JSONObject jsonObject=new JSONObject(inputStream);
StringPoolBuilder poolBuilder = new StringPoolBuilder();
JSONObject jsonObject = new JSONObject(inputStream);
poolBuilder.build(jsonObject);
poolBuilder.apply(tableBlock);
tableBlock.fromJson(jsonObject);
}catch (JSONException ex){
throw new IOException(inputSource.getAlias()+": "+ex.getMessage());
throw new IOException(inputSource.getAlias(), ex);
}
mCache=tableBlock;
mCache = tableBlock;
return tableBlock;
}
TableBlock newInstance(){
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/reandroid/apk/StringPoolBuilder.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/*
* Copyright (C) 2022 github.com/REAndroid
*
* Licensed under the Apache License, Version 2.0 (the "License");
Expand Down
14 changes: 8 additions & 6 deletions src/main/java/com/reandroid/arsc/item/StringItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -254,15 +254,17 @@ public JSONObject toJson() {
if(isNull()){
return null;
}
StyleItem styleItem=getStyle();
if(styleItem == null){
return null;
}
JSONObject jsonObject=new JSONObject();
jsonObject.put(NAME_string, get());
StyleItem styleItem=getStyle();
if(styleItem!=null){
JSONObject styleJson=styleItem.toJson();
if(styleJson!=null){
jsonObject.put(NAME_style, styleJson);
}
JSONObject styleJson = styleItem.toJson();
if(styleJson == null){
return null;
}
jsonObject.put(NAME_style, styleJson);
return jsonObject;
}
@Override
Expand Down
35 changes: 28 additions & 7 deletions src/main/java/com/reandroid/arsc/pool/JsonStringPoolHelper.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
/*
* Copyright (C) 2022 github.com/REAndroid
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.reandroid.arsc.pool;

import com.reandroid.arsc.array.StringArray;
Expand Down Expand Up @@ -88,15 +103,21 @@ public String toString(){
}
static List<StyledString> fromJson(JSONArray jsonArray){
int length = jsonArray.length();
List<StyledString> results=new ArrayList<>();
for(int i=0;i<length;i++){
StyledString styledString=fromJson(jsonArray.getJSONObject(i));
results.add(styledString);
List<StyledString> results = new ArrayList<>(length);
for(int i=0; i < length; i++){
StyledString styledString =
fromJson(jsonArray.getJSONObject(i));
if(styledString != null){
results.add(styledString);
}
}
return results;
}
static StyledString fromJson(JSONObject jsonObject){
String text= jsonObject.getString(StringItem.NAME_string);
private static StyledString fromJson(JSONObject jsonObject){
if(!jsonObject.has(StringItem.NAME_style)){
return null;
}
String text = jsonObject.getString(StringItem.NAME_string);
JSONObject style=jsonObject.getJSONObject(StringItem.NAME_style);
JSONArray spansArray=style.getJSONArray(StyleItem.NAME_spans);
List<StyleSpanInfo> spanInfoList = toSpanInfoList(spansArray);
Expand All @@ -107,7 +128,7 @@ private static List<StyleSpanInfo> toSpanInfoList(JSONArray jsonArray){
List<StyleSpanInfo> results=new ArrayList<>(length);
for(int i=0;i<length;i++){
JSONObject jsonObject = jsonArray.getJSONObject(i);
StyleSpanInfo spanInfo=new StyleSpanInfo(null, 0,0);
StyleSpanInfo spanInfo = new StyleSpanInfo(null, 0,0);
spanInfo.fromJson(jsonObject);
results.add(spanInfo);
}
Expand Down

1 comment on commit e8fff62

@REAndroid
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.