[JSON] fix: convert only styled strings

This commit is contained in:
REAndroid 2023-05-02 12:12:58 +02:00
parent 3289d33c75
commit e8fff620f7
4 changed files with 45 additions and 22 deletions

View File

@ -1,4 +1,4 @@
/* /*
* Copyright (C) 2022 github.com/REAndroid * Copyright (C) 2022 github.com/REAndroid
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
@ -52,22 +52,22 @@ public class SingleJsonTableInputSource extends InputSource {
return outputStream.getCrcValue(); return outputStream.getCrcValue();
} }
public TableBlock getTableBlock() throws IOException{ public TableBlock getTableBlock() throws IOException{
if(mCache!=null){ if(mCache != null){
return mCache; return mCache;
} }
logMessage("Building resources table: "+inputSource.getAlias()); logMessage("Building resources table: " + inputSource.getAlias());
TableBlock tableBlock=newInstance(); TableBlock tableBlock=newInstance();
InputStream inputStream=inputSource.openStream(); InputStream inputStream = inputSource.openStream();
try{ try{
StringPoolBuilder poolBuilder=new StringPoolBuilder(); StringPoolBuilder poolBuilder = new StringPoolBuilder();
JSONObject jsonObject=new JSONObject(inputStream); JSONObject jsonObject = new JSONObject(inputStream);
poolBuilder.build(jsonObject); poolBuilder.build(jsonObject);
poolBuilder.apply(tableBlock); poolBuilder.apply(tableBlock);
tableBlock.fromJson(jsonObject); tableBlock.fromJson(jsonObject);
}catch (JSONException ex){ }catch (JSONException ex){
throw new IOException(inputSource.getAlias()+": "+ex.getMessage()); throw new IOException(inputSource.getAlias(), ex);
} }
mCache=tableBlock; mCache = tableBlock;
return tableBlock; return tableBlock;
} }
TableBlock newInstance(){ TableBlock newInstance(){

View File

@ -1,4 +1,4 @@
/* /*
* Copyright (C) 2022 github.com/REAndroid * Copyright (C) 2022 github.com/REAndroid
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");

View File

@ -254,15 +254,17 @@ public class StringItem extends BlockItem implements JSONConvert<JSONObject> {
if(isNull()){ if(isNull()){
return null; return null;
} }
StyleItem styleItem=getStyle();
if(styleItem == null){
return null;
}
JSONObject jsonObject=new JSONObject(); JSONObject jsonObject=new JSONObject();
jsonObject.put(NAME_string, get()); jsonObject.put(NAME_string, get());
StyleItem styleItem=getStyle(); JSONObject styleJson = styleItem.toJson();
if(styleItem!=null){ if(styleJson == null){
JSONObject styleJson=styleItem.toJson(); return null;
if(styleJson!=null){
jsonObject.put(NAME_style, styleJson);
}
} }
jsonObject.put(NAME_style, styleJson);
return jsonObject; return jsonObject;
} }
@Override @Override

View File

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