Skip to content

Commit

Permalink
improved fastjson 1.x compatbile, support ExtraProcessor, for issue #583
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Oct 4, 2022
1 parent b4ab118 commit 8babe3e
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ public T readJSONBObject(JSONReader jsonReader, Type fieldType, Object fieldName
}
}
if (fieldReader == null) {
jsonReader.skipValue();
processExtra(jsonReader, object);
continue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,11 +170,7 @@ public void readObject(JSONReader jsonReader, Object object, long features) {
}

if (fieldReader == null) {
if (this instanceof ObjectReaderBean) {
processExtra(jsonReader, object);
} else {
jsonReader.skipValue();
}
processExtra(jsonReader, object);
continue;
}

Expand Down Expand Up @@ -298,11 +294,7 @@ public T readObject(JSONReader jsonReader, Type fieldType, Object fieldName, lon
}

if (fieldReader == null) {
if (this instanceof ObjectReaderBean) {
processExtra(jsonReader, object);
} else {
jsonReader.skipValue();
}
processExtra(jsonReader, object);
continue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1116,8 +1116,11 @@ private <T> void genMethodReadJSONBObject(
mw.visitLabel(next_);
}

mw.visitVarInsn(Opcodes.ALOAD, THIS);
mw.visitVarInsn(Opcodes.ALOAD, JSON_READER);
mw.visitMethodInsn(Opcodes.INVOKEVIRTUAL, TYPE_JSON_READER, "skipValue", "()V", false);
mw.visitVarInsn(Opcodes.ALOAD, OBJECT);
mw.visitMethodInsn(Opcodes.INVOKEVIRTUAL, TYPE_OBJECT_READER_ADAPTER, "processExtra", METHOD_DESC_PROCESS_EXTRA, false);

mw.visitJumpInsn(Opcodes.GOTO, for_inc_i_); // continue
} else {
// use switch
Expand Down Expand Up @@ -1227,8 +1230,12 @@ private <T> void genMethodReadJSONBObject(
mw.visitJumpInsn(Opcodes.GOTO, for_inc_i_); // continue

mw.visitLabel(fieldReaderNull_);

mw.visitVarInsn(Opcodes.ALOAD, THIS);
mw.visitVarInsn(Opcodes.ALOAD, JSON_READER);
mw.visitMethodInsn(Opcodes.INVOKEVIRTUAL, TYPE_JSON_READER, "skipValue", "()V", false);
mw.visitVarInsn(Opcodes.ALOAD, OBJECT);
mw.visitMethodInsn(Opcodes.INVOKEVIRTUAL, TYPE_OBJECT_READER_ADAPTER, "processExtra", METHOD_DESC_PROCESS_EXTRA, false);

mw.visitJumpInsn(Opcodes.GOTO, for_inc_i_); // continue
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public T readJSONBObject(JSONReader jsonReader, Type fieldType, Object fieldName

FieldReader fieldReader = getFieldReader(hashCode);
if (fieldReader == null) {
jsonReader.skipValue();
processExtra(jsonReader, null);
continue;
}

Expand Down Expand Up @@ -270,7 +270,7 @@ public T readObject(JSONReader jsonReader, Type fieldType, Object fieldName, lon
}

if (fieldReader == null) {
jsonReader.skipValue();
processExtra(jsonReader, null);
continue;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.alibaba.fastjson2.v1issues.issue_1200;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONB;
import com.alibaba.fastjson2.filter.ExtraProcessor;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -33,6 +34,28 @@ public void processExtra(Object object, String key, Object value) {
assertEquals(2, count.intValue());
}

@Test
public void test_for_issue_jsonb() throws Exception {
String json = "{\"a\":1,\"b\":2}";
byte[] jsonbBytes = JSON.parseObject(json).toJSONBBytes();

final AtomicInteger count = new AtomicInteger(0);
ExtraProcessor extraProcessor = new ExtraProcessor() {
public void processExtra(Object object, String key, Object value) {
System.out.println("setter not found, class " + object.getClass().getName() + ", property " + key);
count.incrementAndGet();
}
};

A a = JSONB.parseObject(jsonbBytes, A.class, extraProcessor);
assertEquals(1, a.a);
assertEquals(1, count.intValue());

B b = JSON.parseObject(json, B.class, extraProcessor);
assertEquals(1, b.a);
assertEquals(2, count.intValue());
}

public static class A {
public int a;
}
Expand Down

0 comments on commit 8babe3e

Please sign in to comment.