[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

@ -65,7 +65,7 @@ public class SingleJsonTableInputSource extends InputSource {
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;

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();
if(styleItem!=null){
JSONObject styleJson = styleItem.toJson(); JSONObject styleJson = styleItem.toJson();
if(styleJson!=null){ if(styleJson == null){
return 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,14 +103,20 @@ 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 =
fromJson(jsonArray.getJSONObject(i));
if(styledString != null){
results.add(styledString); results.add(styledString);
} }
}
return results; return results;
} }
static StyledString fromJson(JSONObject jsonObject){ private static StyledString fromJson(JSONObject jsonObject){
if(!jsonObject.has(StringItem.NAME_style)){
return null;
}
String text = jsonObject.getString(StringItem.NAME_string); 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);