/src/test/java/com/alibaba/json/bvt/naming/NamingSerTest.java

https://github.com/alibaba/fastjson · Java · 90 lines · 68 code · 22 blank · 0 comment · 0 complexity · 9a6dfcf41abd750896a9f3370afa2f57 MD5 · raw file

  1. package com.alibaba.json.bvt.naming;
  2. import org.junit.Assert;
  3. import com.alibaba.fastjson.JSON;
  4. import com.alibaba.fastjson.PropertyNamingStrategy;
  5. import com.alibaba.fastjson.parser.ParserConfig;
  6. import com.alibaba.fastjson.serializer.SerializeConfig;
  7. import junit.framework.TestCase;
  8. public class NamingSerTest extends TestCase {
  9. public void test_snake() throws Exception {
  10. SerializeConfig config = new SerializeConfig();
  11. config.propertyNamingStrategy = PropertyNamingStrategy.SnakeCase;
  12. Model model = new Model();
  13. model.personId = 1001;
  14. String text = JSON.toJSONString(model, config);
  15. Assert.assertEquals("{\"person_id\":1001}", text);
  16. ParserConfig parserConfig = new ParserConfig();
  17. parserConfig.propertyNamingStrategy = PropertyNamingStrategy.SnakeCase;
  18. Model model2 = JSON.parseObject(text, Model.class, parserConfig);
  19. Assert.assertEquals(model.personId, model2.personId);
  20. Model model3 = JSON.parseObject(text, Model.class);
  21. Assert.assertEquals(model.personId, model3.personId);
  22. }
  23. public void test_kebab() throws Exception {
  24. SerializeConfig config = new SerializeConfig();
  25. config.propertyNamingStrategy = PropertyNamingStrategy.KebabCase;
  26. Model model = new Model();
  27. model.personId = 1001;
  28. String text = JSON.toJSONString(model, config);
  29. Assert.assertEquals("{\"person-id\":1001}", text);
  30. ParserConfig parserConfig = new ParserConfig();
  31. parserConfig.propertyNamingStrategy = PropertyNamingStrategy.KebabCase;
  32. Model model2 = JSON.parseObject(text, Model.class, parserConfig);
  33. Assert.assertEquals(model.personId, model2.personId);
  34. Model model3 = JSON.parseObject(text, Model.class);
  35. Assert.assertEquals(model.personId, model3.personId);
  36. }
  37. public void test_pascal() throws Exception {
  38. SerializeConfig config = new SerializeConfig();
  39. config.propertyNamingStrategy = PropertyNamingStrategy.PascalCase;
  40. Model model = new Model();
  41. model.personId = 1001;
  42. String text = JSON.toJSONString(model, config);
  43. Assert.assertEquals("{\"PersonId\":1001}", text);
  44. ParserConfig parserConfig = new ParserConfig();
  45. parserConfig.propertyNamingStrategy = PropertyNamingStrategy.PascalCase;
  46. Model model2 = JSON.parseObject(text, Model.class, parserConfig);
  47. Assert.assertEquals(model.personId, model2.personId);
  48. Model model3 = JSON.parseObject(text, Model.class);
  49. Assert.assertEquals(model.personId, model3.personId);
  50. }
  51. public void test_camel() throws Exception {
  52. SerializeConfig config = new SerializeConfig();
  53. config.propertyNamingStrategy = PropertyNamingStrategy.CamelCase;
  54. Model model = new Model();
  55. model.personId = 1001;
  56. String text = JSON.toJSONString(model, config);
  57. Assert.assertEquals("{\"personId\":1001}", text);
  58. ParserConfig parserConfig = new ParserConfig();
  59. parserConfig.propertyNamingStrategy = PropertyNamingStrategy.CamelCase;
  60. Model model2 = JSON.parseObject(text, Model.class, parserConfig);
  61. Assert.assertEquals(model.personId, model2.personId);
  62. Model model3 = JSON.parseObject(text, Model.class);
  63. Assert.assertEquals(model.personId, model3.personId);
  64. }
  65. public static class Model {
  66. public int personId;
  67. }
  68. }