Skip to content

Commit

Permalink
improved codegen
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Jun 19, 2023
1 parent d5049ee commit 1934f79
Show file tree
Hide file tree
Showing 30 changed files with 1,976 additions and 161 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.alibaba.fastjson2.internal.processor.annotation;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.annotation.JSONCompiled;
import com.alibaba.fastjson2.annotation.JSONField;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class JSONFieldTest {
@Test
public void test() {
Bean bean = new Bean();
bean.id = 123;

String str = JSON.toJSONString(bean);

Bean bean1 = JSON.parseObject(str, Bean.class);
assertEquals(bean.id, bean1.id);
}

@JSONCompiled
public static class Bean {
@JSONField(name = "userId")
public int id;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.alibaba.fastjson2.internal.processor.collections;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.annotation.JSONCompiled;
import org.junit.jupiter.api.Test;

import java.util.*;
import java.util.concurrent.CopyOnWriteArrayList;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class CollectionTest {
@Test
public void test() {
Bean bean = new Bean();
bean.v01 = new ArrayList();
bean.v02 = new ArrayList();
bean.v03 = new ArrayList();
bean.v04 = new ArrayList();
bean.v05 = new LinkedList();
bean.v06 = new CopyOnWriteArrayList();

String str = JSON.toJSONString(bean);
Bean bean1 = JSON.parseObject(str, Bean.class);
assertEquals(bean.v01, bean1.v01);
assertEquals(bean.v02, bean1.v02);
assertEquals(bean.v03, bean1.v03);
assertEquals(bean.v04, bean1.v04);
assertEquals(bean.v05, bean1.v05);
assertEquals(bean.v06, bean1.v06);
}

@JSONCompiled
public static class Bean {
public Iterable v01;
public Collection v02;
public List v03;
public ArrayList v04;
public LinkedList v05;
public CopyOnWriteArrayList v06;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.alibaba.fastjson2.internal.processor.collections;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.annotation.JSONCompiled;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class JSONArrayTest {
@Test
public void test() {
Bean bean = new Bean();
bean.v01 = new JSONArray();

String str = JSON.toJSONString(bean);
Bean bean1 = JSON.parseObject(str, Bean.class);
assertEquals(bean.v01, bean1.v01);
}

@JSONCompiled
public static class Bean {
public JSONArray v01;
}

@Test
public void test1() {
Bean1 bean = new Bean1();
bean.v01 = new JSONArray();

String str = JSON.toJSONString(bean);
Bean1 bean1 = JSON.parseObject(str, Bean1.class);
assertEquals(bean.v01, bean1.v01);
}

@JSONCompiled(referenceDetect = false)
public static class Bean1 {
public JSONArray v01;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package com.alibaba.fastjson2.internal.processor.eishay;

import com.alibaba.fastjson2.annotation.JSONCompiled;

import java.util.Objects;

@JSONCompiled(referenceDetect = false)
public class Image
implements java.io.Serializable {
private static final long serialVersionUID = 1L;

public enum Size {
SMALL, LARGE
}

private int height;
private Size size;
private String title; // Can be null
private String uri;
private int width;

public Image() {
}

public Image(String uri, String title, int width, int height, Size size) {
this.height = height;
this.title = title;
this.uri = uri;
this.width = width;
this.size = size;
}

public void setUri(String uri) {
this.uri = uri;
}

public void setTitle(String title) {
this.title = title;
}

public void setWidth(int width) {
this.width = width;
}

public void setHeight(int height) {
this.height = height;
}

public void setSize(Size size) {
this.size = size;
}

public String getUri() {
return uri;
}

public String getTitle() {
return title;
}

public int getWidth() {
return width;
}

public int getHeight() {
return height;
}

public Size getSize() {
return size;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Image image = (Image) o;
return height == image.height && width == image.width && size == image.size && Objects.equals(title, image.title) && Objects.equals(uri, image.uri);
}

@Override
public int hashCode() {
return Objects.hash(height, size, title, uri, width);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
package com.alibaba.fastjson2.internal.processor.eishay;

import com.alibaba.fastjson2.annotation.JSONCompiled;

import java.util.List;
import java.util.Objects;

@JSONCompiled(referenceDetect = false)
public class Media
implements java.io.Serializable {
public enum Player {
JAVA, FLASH
}

private int bitrate;
private long duration;
private String format;
private int height;
private List<String> persons;
private Player player;
private long size;
private String title;
private String uri;
private int width;
private String copyright;

public Media() {
}

public Media(
String uri,
String title,
int width,
int height,
String format,
long duration,
long size,
int bitrate,
List<String> persons,
Player player,
String copyright
) {
this.uri = uri;
this.title = title;
this.width = width;
this.height = height;
this.format = format;
this.duration = duration;
this.size = size;
this.bitrate = bitrate;
this.persons = persons;
this.player = player;
this.copyright = copyright;
}

public String getUri() {
return uri;
}

public void setUri(String uri) {
this.uri = uri;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public int getWidth() {
return width;
}

public void setWidth(int width) {
this.width = width;
}

public int getHeight() {
return height;
}

public void setHeight(int height) {
this.height = height;
}

public String getFormat() {
return format;
}

public void setFormat(String format) {
this.format = format;
}

public long getDuration() {
return duration;
}

public void setDuration(long duration) {
this.duration = duration;
}

public long getSize() {
return size;
}

public void setSize(long size) {
this.size = size;
}

public int getBitrate() {
return bitrate;
}

public void setBitrate(int bitrate) {
this.bitrate = bitrate;
}

public List<String> getPersons() {
return persons;
}

public void setPersons(List<String> persons) {
this.persons = persons;
}

public Player getPlayer() {
return player;
}

public void setPlayer(Player player) {
this.player = player;
}

public String getCopyright() {
return copyright;
}

public void setCopyright(String copyright) {
this.copyright = copyright;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Media media = (Media) o;
return bitrate == media.bitrate && duration == media.duration && height == media.height && size == media.size && width == media.width && Objects.equals(format, media.format) && Objects.equals(persons, media.persons) && player == media.player && Objects.equals(title, media.title) && Objects.equals(uri, media.uri) && Objects.equals(copyright, media.copyright);
}

@Override
public int hashCode() {
return Objects.hash(bitrate, duration, format, height, persons, player, size, title, uri, width, copyright);
}
}
Loading

0 comments on commit 1934f79

Please sign in to comment.