Skip to content

Commit

Permalink
bug fixd
Browse files Browse the repository at this point in the history
  • Loading branch information
smuyyh committed Nov 30, 2017
1 parent 54d412e commit ae2c0ee
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 32 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Android JSON viewer, to convert JSON Strings to a Friendly Readable Format, it s
## Dependencies

```
compile 'com.yuyh.json:jsonviewer:1.0.2'
compile 'com.yuyh.json:jsonviewer:1.0.3'
```

## Usage
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/assets/demo.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"xxx": {},
"current": {
"feelsLike": {
"unit": "",
Expand Down Expand Up @@ -713,7 +714,6 @@
"status": 0
},
"alerts": [

],
"yesterday": {
"aqi": "53",
Expand Down
4 changes: 2 additions & 2 deletions jsonviewer/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ android {
defaultConfig {
minSdkVersion 11
targetSdkVersion 26
versionCode 1
versionName "1.0.0"
versionCode 4
versionName "1.0.3"
}

buildTypes {
Expand Down
2 changes: 1 addition & 1 deletion jsonviewer/publish.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ apply plugin: 'com.jfrog.bintray'
def siteUrl = 'https://github.com/smuyyh/JsonViewer' // Git项目主页
def gitUrl = 'https://github.com/smuyyh/JsonViewer.git' // Git仓库url
group = "com.yuyh.json" // 一般为包名
version = "1.0.2" // 版本号
version = "1.0.3" // 版本号

install {
repositories.mavenInstaller {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,27 +58,27 @@ public void bindJson(JSONObject object) {
}

public void setKeyColor(int color) {
BaseJsonViewerAdapter.keySpan = new ForegroundColorSpan(color);
BaseJsonViewerAdapter.KEY_COLOR = color;
}

public void setValueTextColor(int color) {
BaseJsonViewerAdapter.textSpan = new ForegroundColorSpan(color);
BaseJsonViewerAdapter.TEXT_COLOR = color;
}

public void setValueNumberColor(int color) {
BaseJsonViewerAdapter.numberSpan = new ForegroundColorSpan(color);
BaseJsonViewerAdapter.NUMBER_COLOR = color;
}

public void setValueUrlColor(int color) {
BaseJsonViewerAdapter.urlSpan = new ForegroundColorSpan(color);
BaseJsonViewerAdapter.URL_COLOR = color;
}

public void setValueNullColor(int color) {
BaseJsonViewerAdapter.nullSpan = new ForegroundColorSpan(color);
BaseJsonViewerAdapter.NUMBER_COLOR = color;
}

public void setBracesColor(int color) {
BaseJsonViewerAdapter.bracesSpan = new ForegroundColorSpan(color);
BaseJsonViewerAdapter.BRACES_COLOR = color;
}

public void setTextSize(int sizeDP) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
*/
public abstract class BaseJsonViewerAdapter<VH extends RecyclerView.ViewHolder> extends RecyclerView.Adapter<VH> {

public static ForegroundColorSpan keySpan = new ForegroundColorSpan(0xFF922799);
public static ForegroundColorSpan textSpan = new ForegroundColorSpan(0xFF3AB54A);
public static ForegroundColorSpan numberSpan = new ForegroundColorSpan(0xFF25AAE2);
public static ForegroundColorSpan urlSpan = new ForegroundColorSpan(0xFF66D2D5);
public static ForegroundColorSpan nullSpan = new ForegroundColorSpan(0xFFEF5935);
public static ForegroundColorSpan bracesSpan = new ForegroundColorSpan(0xFF4A555F);
public static int KEY_COLOR = 0xFF922799;
public static int TEXT_COLOR = 0xFF3AB54A;
public static int NUMBER_COLOR = 0xFF25AAE2;
public static int URL_COLOR = 0xFF66D2D5;
public static int NULL_COLOR = 0xFFEF5935;
public static int BRACES_COLOR = 0xFF4A555F;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.support.v7.widget.RecyclerView;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.view.ViewGroup;

Expand Down Expand Up @@ -75,6 +76,8 @@ public void onBindViewHolder(JsonItemViewHolder holder, int position) {
itemView.hideIcon();
itemView.showRight("}");
return;
} else if (mJSONObject.names() == null) {
return;
}

String key = mJSONObject.names().optString(position - 1); // 遍历key
Expand All @@ -92,8 +95,7 @@ public void onBindViewHolder(JsonItemViewHolder holder, int position) {
itemView.hideIcon();
itemView.showRight("[");
return;
}
if (position == getItemCount() - 1) {
} else if (position == getItemCount() - 1) {
itemView.hideLeft();
itemView.hideIcon();
itemView.showRight("]");
Expand All @@ -112,7 +114,11 @@ public void onBindViewHolder(JsonItemViewHolder holder, int position) {
@Override
public int getItemCount() {
if (mJSONObject != null) {
return mJSONObject.names().length() + 2;
if (mJSONObject.names() != null) {
return mJSONObject.names().length() + 2;
} else {
return 2;
}
}
if (mJSONArray != null) {
return mJSONArray.length() + 2;
Expand All @@ -132,8 +138,8 @@ public int getItemCount() {
private void handleJsonObject(String key, Object value, JsonItemView itemView, boolean appendComma, int hierarchy) {
SpannableStringBuilder keyBuilder = new SpannableStringBuilder(Utils.getHierarchyStr(hierarchy));
keyBuilder.append("\"").append(key).append("\"").append(":");
keyBuilder.setSpan(keySpan, 0, keyBuilder.length() - 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
keyBuilder.setSpan(bracesSpan, keyBuilder.length() - 1, keyBuilder.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
keyBuilder.setSpan(new ForegroundColorSpan(KEY_COLOR), 0, keyBuilder.length() - 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
keyBuilder.setSpan(new ForegroundColorSpan(BRACES_COLOR), keyBuilder.length() - 1, keyBuilder.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

itemView.showLeft(keyBuilder);

Expand Down Expand Up @@ -164,32 +170,34 @@ private void handleValue(Object value, JsonItemView itemView, boolean appendComm
SpannableStringBuilder valueBuilder = new SpannableStringBuilder();
if (value instanceof Number) {
valueBuilder.append(value.toString());
valueBuilder.setSpan(numberSpan, 0, valueBuilder.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
valueBuilder.setSpan(new ForegroundColorSpan(NUMBER_COLOR), 0, valueBuilder.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
} else if (value instanceof JSONObject) {
itemView.showIcon(true);
valueBuilder.append("Object{...}");
valueBuilder.setSpan(new ForegroundColorSpan(BRACES_COLOR), 0, valueBuilder.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
itemView.setIconClickListener(new JsonItemClickListener(value, itemView, appendComma, hierarchy + 1));
} else if (value instanceof JSONArray) {
itemView.showIcon(true);
valueBuilder.append("Array[").append(String.valueOf(((JSONArray) value).length())).append("]");
valueBuilder.setSpan(bracesSpan, 0, 6, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
valueBuilder.setSpan(numberSpan, 6, valueBuilder.length() - 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
valueBuilder.setSpan(bracesSpan, valueBuilder.length() - 1, valueBuilder.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
int len = valueBuilder.length();
valueBuilder.setSpan(new ForegroundColorSpan(BRACES_COLOR), 0, 6, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
valueBuilder.setSpan(new ForegroundColorSpan(NUMBER_COLOR), 6, len - 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
valueBuilder.setSpan(new ForegroundColorSpan(BRACES_COLOR), len - 1, len, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
itemView.setIconClickListener(new JsonItemClickListener(value, itemView, appendComma, hierarchy + 1));
} else if (value instanceof String) {
itemView.hideIcon();
valueBuilder.append("\"").append(value.toString()).append("\"");
if (Utils.isUrl(value.toString())) {
valueBuilder.setSpan(textSpan, 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
valueBuilder.setSpan(urlSpan, 1, valueBuilder.length() - 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
valueBuilder.setSpan(textSpan, valueBuilder.length() - 1, valueBuilder.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
valueBuilder.setSpan(new ForegroundColorSpan(TEXT_COLOR), 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
valueBuilder.setSpan(new ForegroundColorSpan(URL_COLOR), 1, valueBuilder.length() - 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
valueBuilder.setSpan(new ForegroundColorSpan(TEXT_COLOR), valueBuilder.length() - 1, valueBuilder.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
} else {
valueBuilder.setSpan(textSpan, 0, valueBuilder.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
valueBuilder.setSpan(new ForegroundColorSpan(TEXT_COLOR), 0, valueBuilder.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
} else if (valueBuilder.length() == 0 || value == null) {
itemView.hideIcon();
valueBuilder.append("null");
valueBuilder.setSpan(nullSpan, 0, valueBuilder.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
valueBuilder.setSpan(new ForegroundColorSpan(NULL_COLOR), 0, valueBuilder.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
if (appendComma) {
valueBuilder.append(",");
Expand Down Expand Up @@ -224,7 +232,7 @@ public void onClick(View view) {
itemView.setTag(itemView.getRightText());
itemView.showRight(isJsonArray ? "[" : "{");
JSONArray array = isJsonArray ? (JSONArray) value : ((JSONObject) value).names();
for (int i = 0; i < array.length(); i++) {
for (int i = 0; array != null && i < array.length(); i++) {
JsonItemView childItemView = new JsonItemView(itemView.getContext());
Object childValue = array.opt(i);
if (isJsonArray) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import android.widget.TextView;

import com.yuyh.jsonviewer.library.R;
import com.yuyh.jsonviewer.library.adapter.BaseJsonViewerAdapter;

/**
* Created by yuyuhang on 2017/11/29.
Expand Down Expand Up @@ -55,6 +56,7 @@ private void initView() {

mTvLeft.setTextSize(TEXT_SIZE_DP);
mTvRight.setTextSize(TEXT_SIZE_DP);
mTvRight.setTextColor(BaseJsonViewerAdapter.BRACES_COLOR);
mTvIcon.setTextSize(TEXT_SIZE_DP - 4);
}

Expand Down

0 comments on commit ae2c0ee

Please sign in to comment.