|
| 1 | +/* |
| 2 | + * Copyright 2015 Igor Maznitsa. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.igormaznitsa.jbbp.it; |
| 17 | + |
| 18 | +import com.igormaznitsa.jbbp.JBBPParser; |
| 19 | +import com.igormaznitsa.jbbp.model.*; |
| 20 | +import java.io.InputStream; |
| 21 | +import net.minidev.json.*; |
| 22 | +import org.junit.Test; |
| 23 | + |
| 24 | +public class ConvertToJSONTest extends AbstractParserIntegrationTest { |
| 25 | + |
| 26 | + public static JSONObject convertToJSon(final JSONObject jsn, final JBBPAbstractField field) { |
| 27 | + final JSONObject json = jsn == null ? new JSONObject() : jsn; |
| 28 | + |
| 29 | + final String fieldName = field.getFieldName() == null ? "nonamed" : field.getFieldName(); |
| 30 | + if (field instanceof JBBPAbstractArrayField) { |
| 31 | + final JSONArray jsonArray = new JSONArray(); |
| 32 | + if (field instanceof JBBPFieldArrayBit) { |
| 33 | + for (final byte b : ((JBBPFieldArrayBit) field).getArray()) { |
| 34 | + jsonArray.add(b); |
| 35 | + } |
| 36 | + } |
| 37 | + else if (field instanceof JBBPFieldArrayBoolean) { |
| 38 | + for (final boolean b : ((JBBPFieldArrayBoolean) field).getArray()) { |
| 39 | + jsonArray.add(b); |
| 40 | + } |
| 41 | + } |
| 42 | + else if (field instanceof JBBPFieldArrayByte) { |
| 43 | + for (final byte b : ((JBBPFieldArrayByte) field).getArray()) { |
| 44 | + jsonArray.add(b); |
| 45 | + } |
| 46 | + } |
| 47 | + else if (field instanceof JBBPFieldArrayInt) { |
| 48 | + for (final int b : ((JBBPFieldArrayInt) field).getArray()) { |
| 49 | + jsonArray.add(b); |
| 50 | + } |
| 51 | + } |
| 52 | + else if (field instanceof JBBPFieldArrayLong) { |
| 53 | + for (final long b : ((JBBPFieldArrayLong) field).getArray()) { |
| 54 | + jsonArray.add(b); |
| 55 | + } |
| 56 | + } |
| 57 | + else if (field instanceof JBBPFieldArrayShort) { |
| 58 | + for (final short b : ((JBBPFieldArrayShort) field).getArray()) { |
| 59 | + jsonArray.add(b); |
| 60 | + } |
| 61 | + } |
| 62 | + else if (field instanceof JBBPFieldArrayStruct) { |
| 63 | + final JBBPFieldArrayStruct array = (JBBPFieldArrayStruct) field; |
| 64 | + for (int i = 0; i < array.size(); i++) { |
| 65 | + jsonArray.add(convertToJSon(new JSONObject(), array.getElementAt(i))); |
| 66 | + } |
| 67 | + } |
| 68 | + else if (field instanceof JBBPFieldArrayUByte) { |
| 69 | + for (final byte b : ((JBBPFieldArrayUByte) field).getArray()) { |
| 70 | + jsonArray.add(b & 0xFF); |
| 71 | + } |
| 72 | + } |
| 73 | + else if (field instanceof JBBPFieldArrayUShort) { |
| 74 | + for (final short b : ((JBBPFieldArrayUShort) field).getArray()) { |
| 75 | + jsonArray.add(b & 0xFFFF); |
| 76 | + } |
| 77 | + } |
| 78 | + else { |
| 79 | + throw new Error("Unexpected field type"); |
| 80 | + } |
| 81 | + json.put(fieldName, jsonArray); |
| 82 | + } |
| 83 | + else { |
| 84 | + if (field instanceof JBBPFieldBit) { |
| 85 | + json.put(fieldName, ((JBBPFieldBit) field).getAsInt()); |
| 86 | + } |
| 87 | + else if (field instanceof JBBPFieldBoolean) { |
| 88 | + json.put(fieldName, ((JBBPFieldBoolean) field).getAsBool()); |
| 89 | + } |
| 90 | + else if (field instanceof JBBPFieldByte) { |
| 91 | + json.put(fieldName, ((JBBPFieldByte) field).getAsInt()); |
| 92 | + } |
| 93 | + else if (field instanceof JBBPFieldInt) { |
| 94 | + json.put(fieldName, ((JBBPFieldInt) field).getAsInt()); |
| 95 | + } |
| 96 | + else if (field instanceof JBBPFieldLong) { |
| 97 | + json.put(fieldName, ((JBBPFieldLong) field).getAsLong()); |
| 98 | + } |
| 99 | + else if (field instanceof JBBPFieldShort) { |
| 100 | + json.put(fieldName, ((JBBPFieldShort) field).getAsInt()); |
| 101 | + } |
| 102 | + else if (field instanceof JBBPFieldStruct) { |
| 103 | + final JBBPFieldStruct struct = (JBBPFieldStruct) field; |
| 104 | + final JSONObject obj = new JSONObject(); |
| 105 | + for (final JBBPAbstractField f : struct.getArray()) { |
| 106 | + convertToJSon(obj, f); |
| 107 | + } |
| 108 | + if (jsn == null) { |
| 109 | + json.putAll(obj); |
| 110 | + } |
| 111 | + else { |
| 112 | + json.put(fieldName, obj); |
| 113 | + } |
| 114 | + } |
| 115 | + else if (field instanceof JBBPFieldUByte) { |
| 116 | + json.put(fieldName, ((JBBPFieldUByte) field).getAsInt()); |
| 117 | + } |
| 118 | + else if (field instanceof JBBPFieldUShort) { |
| 119 | + json.put(fieldName, ((JBBPFieldUShort) field).getAsInt()); |
| 120 | + } |
| 121 | + else { |
| 122 | + throw new Error("Unexpected field"); |
| 123 | + } |
| 124 | + } |
| 125 | + return json; |
| 126 | + } |
| 127 | + |
| 128 | + @Test |
| 129 | + public void testConvertToJSON() throws Exception { |
| 130 | + final InputStream pngStream = getResourceAsInputStream("picture.png"); |
| 131 | + try { |
| 132 | + |
| 133 | + final JBBPParser pngParser = JBBPParser.prepare( |
| 134 | + "long header;" |
| 135 | + + "// chunks\n" |
| 136 | + + "chunk [_]{" |
| 137 | + + " int length; " |
| 138 | + + " int type; " |
| 139 | + + " byte[length] data; " |
| 140 | + + " int crc;" |
| 141 | + + "}" |
| 142 | + ); |
| 143 | + |
| 144 | + final JSONObject json = convertToJSon(null, pngParser.parse(pngStream)); |
| 145 | + final String jsonText = json.toJSONString(JSONStyle.MAX_COMPRESS); |
| 146 | + System.out.println(jsonText); |
| 147 | + assertFile("pnginjson.txt", jsonText); |
| 148 | + } |
| 149 | + finally { |
| 150 | + pngStream.close(); |
| 151 | + } |
| 152 | + } |
| 153 | +} |
0 commit comments