This repository has been archived by the owner on Nov 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
在model对象和json字符串之间相互转换时可以设置model属性名的格式,支持驼峰、大小写下划线三种格式
- Loading branch information
1 parent
dfd2bd9
commit 073c656
Showing
14 changed files
with
220 additions
and
30 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
71 changes: 71 additions & 0 deletions
71
test/src/test/java/org/lealone/plugins/test/orm/OrmJsonTest.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* Copyright Lealone Database Group. | ||
* Licensed under the Server Side Public License, v 1. | ||
* Initial Developer: zhh | ||
*/ | ||
package org.lealone.plugins.test.orm; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.lealone.plugins.orm.Model.CaseFormat; | ||
import org.lealone.plugins.orm.json.JsonObject; | ||
import org.lealone.plugins.test.orm.generated.JsonTestTable; | ||
import org.lealone.plugins.test.orm.generated.User; | ||
|
||
public class OrmJsonTest extends OrmTestBase { | ||
|
||
@Before | ||
@Override | ||
public void setUpBefore() { | ||
setEmbedded(true); | ||
setInMemory(true); | ||
SqlScript.createUserTable(this); | ||
SqlScript.createJsonTestTable(this); | ||
} | ||
|
||
@Test | ||
public void run() { | ||
testModelType(); | ||
testCaseFormat(); | ||
} | ||
|
||
void testModelType() { | ||
// dao对象序列化后包含modelType字段,并且是ROOT_DAO | ||
JsonObject json = new JsonObject(User.dao.encode()); | ||
assertTrue(json.getInteger("modelType") == User.ROOT_DAO); | ||
|
||
// 反序列化 | ||
String str = json.encode(); | ||
User u = User.decode(str); | ||
assertTrue(u.isDao()); | ||
|
||
// 普通User对象序列化后也包含modelType字段,但为REGULAR_MODEL | ||
json = new JsonObject(new User().encode()); | ||
assertTrue(json.getInteger("modelType") == User.REGULAR_MODEL); | ||
} | ||
|
||
void testCaseFormat() { | ||
User user = new User().id.set(1006).name.set("rob6").phones.set(new Object[] { 1, 2, 3 }); | ||
String json = user.encode(); | ||
JsonObject jsonObject = new JsonObject(json); | ||
assertEquals(jsonObject.getString(User.dao.name.getName()), user.name.get()); | ||
User u = User.decode(json); | ||
assertEquals(u.name.get(), user.name.get()); | ||
|
||
json = user.encode(CaseFormat.CAMEL); | ||
jsonObject = new JsonObject(json); | ||
assertEquals(jsonObject.getString(User.dao.name.getName(CaseFormat.CAMEL)), user.name.get()); | ||
|
||
JsonTestTable t1 = new JsonTestTable().propertyName1.set(1).propertyName2.set(3); | ||
json = t1.encode(); | ||
jsonObject = new JsonObject(json); | ||
assertEquals(jsonObject.getInteger(JsonTestTable.dao.propertyName1.getName(CaseFormat.CAMEL)), | ||
t1.propertyName1.get()); | ||
JsonTestTable t2 = JsonTestTable.decode(json); | ||
assertEquals(t1.propertyName2.get(), t2.propertyName2.get()); | ||
|
||
json = t1.encode(CaseFormat.LOWER_UNDERSCORE); | ||
t2 = JsonTestTable.decode(json, CaseFormat.LOWER_UNDERSCORE); | ||
assertEquals(t1.propertyName2.get(), t2.propertyName2.get()); | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
44 changes: 44 additions & 0 deletions
44
test/src/test/java/org/lealone/plugins/test/orm/generated/JsonTestTable.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package org.lealone.plugins.test.orm.generated; | ||
|
||
import org.lealone.plugins.orm.Model; | ||
import org.lealone.plugins.orm.ModelProperty; | ||
import org.lealone.plugins.orm.ModelTable; | ||
import org.lealone.plugins.orm.property.PInteger; | ||
import org.lealone.plugins.orm.property.PLong; | ||
|
||
/** | ||
* Model for table 'JSON_TEST_TABLE'. | ||
* | ||
* THIS IS A GENERATED OBJECT, DO NOT MODIFY THIS CLASS. | ||
*/ | ||
public class JsonTestTable extends Model<JsonTestTable> { | ||
|
||
public static final JsonTestTable dao = new JsonTestTable(null, ROOT_DAO); | ||
|
||
public final PInteger<JsonTestTable> propertyName1; | ||
public final PLong<JsonTestTable> propertyName2; | ||
|
||
public JsonTestTable() { | ||
this(null, REGULAR_MODEL); | ||
} | ||
|
||
private JsonTestTable(ModelTable t, short modelType) { | ||
super(t == null ? new ModelTable("TEST", "PUBLIC", "JSON_TEST_TABLE") : t, modelType); | ||
propertyName1 = new PInteger<>("PROPERTY_NAME1", this); | ||
propertyName2 = new PLong<>("PROPERTY_NAME2", this); | ||
super.setModelProperties(new ModelProperty[] { propertyName1, propertyName2 }); | ||
} | ||
|
||
@Override | ||
protected JsonTestTable newInstance(ModelTable t, short modelType) { | ||
return new JsonTestTable(t, modelType); | ||
} | ||
|
||
public static JsonTestTable decode(String str) { | ||
return decode(str, null); | ||
} | ||
|
||
public static JsonTestTable decode(String str, CaseFormat format) { | ||
return new JsonTestTable().decode0(str, format); | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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