Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions core/src/main/java/com/alibaba/fastjson2/JSONArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,14 @@ public JSONObject getJSONObject(int index) {
return object;
}

if (value instanceof Map.Entry) {
Map.Entry entry = (Map.Entry) value;
JSONObject object = new JSONObject();
object.put(entry.getKey().toString(), entry.getValue());
set(index, object);
Copy link
Copy Markdown
Member

@wenshao wenshao Apr 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

set(index, 
	JSONObject.of(entry.getKey().toString(), entry.getValue());

=======
Can we do this in a more concise way?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am a beginner. Thank you for your guidance. But I don't know how to modify the PR. Do I need to resubmit it?

return object;
}

Class valueClass = value.getClass();
ObjectWriter objectWriter = JSONFactory.getDefaultObjectWriterProvider().getObjectWriter(valueClass);
if (objectWriter instanceof ObjectWriterAdapter) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,13 @@ public JSONObject toJSONObject(T object, long features) {
if (fieldValue == object) {
fieldValue = jsonObject;
}

if (fieldValue instanceof Enum) {
if ((features & WriteEnumsUsingName.mask) != 0) {
fieldValue = ((Enum) fieldValue).name();
}
}

if (fieldWriter instanceof FieldWriterObject && !(fieldValue instanceof Map)) {
ObjectWriter valueWriter = fieldWriter.getInitWriter();
if (valueWriter == null) {
Expand Down
29 changes: 29 additions & 0 deletions core/src/test/java/com/alibaba/fastjson2/issues_3400/Issue3419
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.alibaba.fastjson2.issues_3400;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import org.junit.jupiter.api.Test;

import java.util.HashMap;
import java.util.Map;

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

public class Issue3419 {
@Test
public void test() {
Map<String, Object> map = new HashMap<>();
map.put("str", new float[] {1.0f, 1.0f, 1.0f, 1.0f, 1.0f});
map.put("str2", new float[] {2.0f, 2.0f, 2.0f, 2.0f, 2.0f});

JSONObject jo = new JSONObject();
JSONArray ja = new JSONArray();
ja.addAll(map.entrySet());
jo.put("content", ja);

JSONObject obj = jo.getJSONArray("content").getJSONObject(0);
assertEquals(obj.toString(), "{\"str\":[1.0,1.0,1.0,1.0,1.0]}");
JSONObject obj2 = jo.getJSONArray("content").getJSONObject(1);
assertEquals(obj2.toString(), "{\"str2\":[2.0,2.0,2.0,2.0,2.0]}");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.alibaba.fastjson2.issues_3400;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.JSONWriter;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.junit.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.concurrent.TimeUnit;

public class Issue3464 {

@Test
public void test() {
Data data = new Data(TimeUnit.MINUTES);
JSONObject json = (JSONObject) JSON.toJSON(data, JSONWriter.Feature.WriteEnumsUsingName);
assertEquals(json.get("unit").getClass().toString(), "class java.lang.String");
}

@Getter
@AllArgsConstructor
private static class Data {
private TimeUnit unit;
}
}