### Question:
JsonUtil { @SuppressWarnings("resource") public static String[] decode(final TextWebSocketFrame frame) throws IOException { final ByteBuf content = frame.content(); if (content.readableBytes() == 0) { return EMPTY_STRING_ARRAY; } final ByteBufInputStream byteBufInputStream = new ByteBufInputStream(content); final byte firstByte = content.getByte(0); if (firstByte == '[') { return MAPPER.readValue(byteBufInputStream, String[].class); } else if (firstByte == '{') { return new String[] { content.toString(CharsetUtil.UTF_8) }; } else { return new String[] { MAPPER.readValue(byteBufInputStream, String.class) }; } } private  JsonUtil(); @SuppressWarnings("resource") static String[] decode(final TextWebSocketFrame frame); static String[] decode(final String content); static String encode(final String content);  }### Answer:
@Test public void decodeArrayWithSimpleStrings() throws Exception { final String[] decode = JsonUtil.decode("[\"one\", \"two\"]"); assertThat(decode.length, is(2)); assertThat(decode[0], equalTo("one")); assertThat(decode[1], equalTo("two")); }
@Test public void decodeArrayWithJsonObject() throws Exception { final String[] decode = JsonUtil.decode("[{\"firstName\":\"Fletch\"}]"); assertThat(decode.length, is(1)); assertThat(decode[0], equalTo("{\"firstName\":\"Fletch\"}")); }
@Test public void decodeArrayWithJsonObjectAndString() throws Exception { final String[] decode = JsonUtil.decode("[{\"firstName\":\"Fletch\"}, \"10\"]"); assertThat(decode.length, is(2)); assertThat(decode[0], equalTo("{\"firstName\":\"Fletch\"}")); assertThat(decode[1], equalTo("10")); }
@Test public void decodeArrayWithJsonObjectAndArray() throws Exception { final String[] decode = JsonUtil.decode("[{\"firstName\":[\"Fletch\"]}, \"10\"]"); assertThat(decode.length, is(2)); assertThat(decode[0], equalTo("{\"firstName\":[\"Fletch\"]}")); assertThat(decode[1], equalTo("10")); }
@Test public void decodeObject() throws Exception { final String[] decode = JsonUtil.decode("{\"firstName\":\"Fletch\"}"); assertThat(decode.length, is(1)); assertThat(decode[0], equalTo("{\"firstName\":\"Fletch\"}")); }
@Test public void decodeString() throws Exception { final String[] decode = JsonUtil.decode("\"x\""); assertThat(decode.length, is(1)); assertThat(decode[0], equalTo("x")); }
@Test public void decodeTextWebSocketFrameSimpleString() throws Exception { final TextWebSocketFrame frame = new TextWebSocketFrame("\"test\""); final String[] decode = JsonUtil.decode(frame); assertThat(decode.length, is(1)); assertThat(decode[0], equalTo("test")); frame.release(); }
@Test public void decodeTextWebSocketFrameArray() throws Exception { final TextWebSocketFrame frame = new TextWebSocketFrame("[\"test\"]"); final String[] decode = JsonUtil.decode(frame); assertThat(decode.length, is(1)); assertThat(decode[0], equalTo("test")); frame.release(); }
@Test public void decodeTextWebSocketFrameObject() throws Exception { final TextWebSocketFrame frame = new TextWebSocketFrame("{\"firstName\":\"Fletch\"}"); final String[] decode = JsonUtil.decode(frame); assertThat(decode.length, is(1)); assertThat(decode[0], equalTo("{\"firstName\":\"Fletch\"}")); frame.release(); }