[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
*
* Licensed under the Apache License, Version 2.0 (the "License");
@ -52,22 +52,22 @@ public class SingleJsonTableInputSource extends InputSource {
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(){

View File

@ -1,4 +1,4 @@
/*
/*
* Copyright (C) 2022 github.com/REAndroid
*
* 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()){
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

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;
import com.reandroid.arsc.array.StringArray;
@ -88,15 +103,21 @@ class JsonStringPoolHelper<T extends StringItem> {
}
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);
@ -107,7 +128,7 @@ class JsonStringPoolHelper<T extends StringItem> {
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);
}