Created
October 10, 2020 21:20
-
-
Save 911992/61936ad985b7b39a6fd35224e8d37eb6 to your computer and use it in GitHub Desktop.
gson MutableJsonReader example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* To change this license header, choose License Headers in Project Properties. | |
* To change this template file, choose Tools | Templates | |
* and open the template in the editor. | |
*/ | |
/* | |
sample_code_java0 | |
File: json_mutable_writer_test.java | |
Created on: Oct 10, 2020 12:43:02 PM | |
@author https://github.com/911992 | |
History: | |
initial version: 0.1(20201010) | |
*/ | |
package json_mutable_test; | |
import com.google.gson.stream.MutableJsonWriter; | |
import java.io.StringWriter; | |
import junit.framework.TestCase; | |
/** | |
* | |
* @author https://github.com/911992 | |
*/ | |
public class json_mutable_writer_test extends TestCase{ | |
public void testMutability() throws Exception{ | |
MutableJsonWriter _shared_writer = new MutableJsonWriter(); | |
final String _a = "happy"; | |
final String _b = "opensource"; | |
String _val = "911"; | |
String _expected_json=String.format("{\"%s\":\"%s\",\"%s\":\"%s\"}", _a,_val,_b,_val); | |
System.out.printf("Run 0:\n"); | |
System.out.printf("Expected json: %s\n",_expected_json); | |
StringWriter _str_writer = new StringWriter(); | |
_shared_writer.reset(_str_writer); | |
_shared_writer.beginObject(); | |
_shared_writer.name(_a); | |
_shared_writer.value(_val); | |
_shared_writer.name(_b); | |
_shared_writer.value(_val); | |
_shared_writer.endObject(); | |
_shared_writer.flush(); | |
_shared_writer.close(); | |
String _gen_json = _str_writer.toString(); | |
System.out.printf("Generated json: %s\n",_gen_json); | |
System.out.printf("Pass:%s\n",Boolean.toString(_expected_json.equals(_gen_json))); | |
assertEquals(_expected_json, _gen_json); | |
System.out.println(""); | |
_val = "992"; | |
_expected_json=String.format("{\"%s\":\"%s\",\"%s\":\"%s\"}", _a,_val,_b,_val); | |
System.out.printf("Run 1:\n"); | |
System.out.printf("Expected json: %s\n",_expected_json); | |
_str_writer = new StringWriter(); | |
_shared_writer.reset(_str_writer); | |
_shared_writer.beginObject(); | |
_shared_writer.name(_a); | |
_shared_writer.value(_val); | |
_shared_writer.name(_b); | |
_shared_writer.value(_val); | |
_shared_writer.endObject(); | |
_shared_writer.flush(); | |
_shared_writer.close(); | |
_gen_json = _str_writer.toString(); | |
System.out.printf("Generated json: %s\n",_gen_json); | |
System.out.printf("Pass:%s\n",Boolean.toString(_expected_json.equals(_gen_json))); | |
assertEquals(_expected_json, _gen_json); | |
} | |
public static void main(String[] args)throws Throwable { | |
new json_mutable_writer_test().testMutability(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment