Skip to content
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONWriter;
import com.google.protobuf.*;
import com.google.protobuf_test_messages.proto3.TestMessagesProto3.TestAllTypesProto3;
import com.google.protobuf.util.JsonFormat;

import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -109,6 +110,20 @@ void wellKnownTypesMatchForConcreteAndDynamicMessages() throws Exception {
}
}

@Test
void repeatedTimestampAndDurationMatchAcrossAllPaths() throws Exception {
var original = TestAllTypesProto3.newBuilder()
.addRepeatedTimestamp(Timestamp.newBuilder().setSeconds(-1).setNanos(123456789))
.addRepeatedTimestamp(Timestamp.newBuilder().setSeconds(1711627200).setNanos(123000000))
.addRepeatedDuration(Duration.newBuilder().setSeconds(-2).setNanos(-123456789))
.addRepeatedDuration(Duration.newBuilder().setSeconds(3).setNanos(250000000)).build();
String expected = PRINTER.print(original);
for (var encoder : encoders()) {
assertEquals(expected, encoder.encode(original));
assertEquals(expected, new String(encoder.encodeToBytes(original), StandardCharsets.UTF_8));
}
}

@Test
void booleanMapKeysStayBooleanNamesWithNumericBooleanFeature() throws Exception {
var message = TestMaps.newBuilder().putBoolToString(true, "yes").putBoolToString(false, "no").build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@
* For Timestamp and Duration, {@code writeTimestampDirect()} and
* {@code writeDurationDirect()} accept primitive seconds/nanos directly,
* bypassing descriptor lookup and {@code message.getField()} reflection. These
* are used by generated encoders that know the field type at generation time.
* are used by generated encoders and pre-specialized typed runtime accessors
* that know the field type before entering the hot write loop.
*
* <p>
* Timestamp formatting uses Howard Hinnant's civil calendar algorithm to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
import com.alibaba.fastjson2.JSONWriter;

/**
* Pre-encoded field name in both UTF-16 (char[]) and UTF-8 (byte[]) forms.
* Dispatches to the optimal variant based on the JSONWriter type.
* Pre-encoded field name in both UTF-16 (char[]) and UTF-8 (byte[]) forms. The
* caller hoists the JSONWriter encoding check once per message and passes the
* selected form to {@link #writeTo(JSONWriter, boolean)}.
*/
public record FieldName(char[] chars, byte[] utf8) {

Expand All @@ -28,8 +29,8 @@ public static FieldName of(String jsonName) {
}
}

public void writeTo(JSONWriter jw) {
if (jw.isUTF8())
public void writeTo(JSONWriter jw, boolean isUtf8) {
if (isUtf8)
jw.writeNameRaw(utf8);
else
jw.writeNameRaw(chars);
Expand Down
Loading
Loading