|
3 | 3 |
|
4 | 4 | import java.util.ArrayList; |
5 | 5 | import java.util.HashMap; |
| 6 | +import java.util.LinkedHashMap; |
6 | 7 | import java.util.List; |
7 | 8 | import java.util.Map; |
8 | 9 | import java.util.Arrays; |
@@ -720,6 +721,84 @@ private String secureUrl(String url) { |
720 | 721 |
|
721 | 722 | } |
722 | 723 |
|
| 724 | + private String stripOldZeroPaddingOfNumericString(String value) { |
| 725 | + if (value == null) { |
| 726 | + return null; |
| 727 | + } |
| 728 | + String sign = ""; |
| 729 | + if (value.startsWith("-")) { |
| 730 | + sign = "-"; |
| 731 | + value = value.substring(1); |
| 732 | + } |
| 733 | + if (!value.matches("0*\\d+(\\.\\d+)?")) { |
| 734 | + return sign + value; |
| 735 | + } |
| 736 | + if (value.matches("0+")) { |
| 737 | + return sign + "0"; |
| 738 | + } |
| 739 | + if (value.matches("0+\\.\\d+")) { |
| 740 | + return sign + "0" + value.substring(value.indexOf('.')); |
| 741 | + } |
| 742 | + return sign + value.replaceFirst("^0+", ""); |
| 743 | + } |
| 744 | + |
| 745 | + private Object sanitizeDisplayValue(String key, Object rawValue) { |
| 746 | + if (rawValue == null) { |
| 747 | + return null; |
| 748 | + } |
| 749 | + if ("expression_level".equals(key)) { |
| 750 | + if (rawValue instanceof String) { |
| 751 | + return stripOldZeroPaddingOfNumericString((String) rawValue); |
| 752 | + } |
| 753 | + } |
| 754 | + if ("dataset_counts".equals(key)) { |
| 755 | + if (rawValue instanceof Map<?, ?>) { |
| 756 | + Map<String, Object> sanitized = new LinkedHashMap<String, Object>(); |
| 757 | + for (Map.Entry<?, ?> entry : ((Map<?, ?>) rawValue).entrySet()) { |
| 758 | + Object value = entry.getValue(); |
| 759 | + if (value instanceof String) { |
| 760 | + value = stripOldZeroPaddingOfNumericString((String) value); |
| 761 | + } else if (value instanceof Map<?, ?> || value instanceof List<?>) { |
| 762 | + value = sanitizeDisplayStructure(value); |
| 763 | + } |
| 764 | + sanitized.put(entry.getKey().toString(), value); |
| 765 | + } |
| 766 | + return sanitized; |
| 767 | + } |
| 768 | + } |
| 769 | + return rawValue; |
| 770 | + } |
| 771 | + |
| 772 | + private Object sanitizeDisplayStructure(Object value) { |
| 773 | + if (value instanceof Map<?, ?>) { |
| 774 | + Map<String, Object> sanitized = new LinkedHashMap<String, Object>(); |
| 775 | + for (Map.Entry<?, ?> entry : ((Map<?, ?>) value).entrySet()) { |
| 776 | + Object child = entry.getValue(); |
| 777 | + if (child instanceof String) { |
| 778 | + child = stripOldZeroPaddingOfNumericString((String) child); |
| 779 | + } else if (child instanceof Map<?, ?> || child instanceof List<?>) { |
| 780 | + child = sanitizeDisplayStructure(child); |
| 781 | + } |
| 782 | + sanitized.put(entry.getKey().toString(), child); |
| 783 | + } |
| 784 | + return sanitized; |
| 785 | + } |
| 786 | + if (value instanceof List<?>) { |
| 787 | + List<Object> sanitizedList = new ArrayList<Object>(); |
| 788 | + for (Object element : (List<?>) value) { |
| 789 | + if (element instanceof String) { |
| 790 | + sanitizedList.add(stripOldZeroPaddingOfNumericString((String) element)); |
| 791 | + } else if (element instanceof Map<?, ?> || element instanceof List<?>) { |
| 792 | + sanitizedList.add(sanitizeDisplayStructure(element)); |
| 793 | + } else { |
| 794 | + sanitizedList.add(element); |
| 795 | + } |
| 796 | + } |
| 797 | + return sanitizedList; |
| 798 | + } |
| 799 | + return value; |
| 800 | + } |
| 801 | + |
723 | 802 | // END VFB term info schema |
724 | 803 |
|
725 | 804 | /* |
@@ -889,7 +968,9 @@ public QueryResults process(ProcessQuery query, DataSource dataSource, Variable |
889 | 968 | hasGeneScore = true; |
890 | 969 | break; |
891 | 970 | } |
892 | | - tempData = new Gson().toJson(results.getValue(key, count)); |
| 971 | + Object rawValue = results.getValue(key, count); |
| 972 | + Object displayValue = sanitizeDisplayValue(key, rawValue); |
| 973 | + tempData = new Gson().toJson(displayValue); |
893 | 974 | json = json + "\"" + key + "\":" + tempData; |
894 | 975 | if (debug){ |
895 | 976 | if (tempData.length() > 1000){ |
|
0 commit comments