Skip to content
This repository was archived by the owner on May 8, 2026. It is now read-only.
Open
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
15 changes: 15 additions & 0 deletions src/main/java/com/marcospassos/phpserializer/Writer.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import java.lang.reflect.Modifier;
import java.nio.charset.Charset;

import com.marcospassos.phpserializer.state.FinishedState;
import com.marcospassos.phpserializer.state.WritingValueState;

Expand Down Expand Up @@ -356,6 +357,20 @@ public void writeKey(int index)
buffer.append(';');
}

/**
* Writes the key of an array entry to the buffer.
*
* @param index The key of the array entry.
*/
public void writeKey(long index)
{
setState(state.key());

buffer.append("i:");
buffer.append(index);
buffer.append(';');
}

/**
* Writes the key of an array entry to the buffer.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.marcospassos.phpserializer.adapter;

import java.util.Map;

import com.marcospassos.phpserializer.Context;
import com.marcospassos.phpserializer.TypeAdapter;
import com.marcospassos.phpserializer.Writer;
Expand All @@ -27,7 +28,9 @@ public void write(Map<K, V> map, Writer writer, Context context)
Object key = entry.getKey();

if (key instanceof Integer) {
writer.writeKey((Integer) key);
writer.writeKey((int) key);
} else if (key instanceof Long) {
writer.writeKey((long) key);
} else {
writer.writeKey(key.toString());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

import java.util.HashMap;
import java.util.Map;
import com.marcospassos.phpserializer.Context;
import com.marcospassos.phpserializer.Writer;

import org.junit.Test;
import org.mockito.InOrder;

import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.mock;

import com.marcospassos.phpserializer.Context;
import com.marcospassos.phpserializer.Writer;

/**
* @author Marcos Passos
* @since 1.0
Expand All @@ -26,14 +27,15 @@ public void write() throws Exception
map.put(1, "a");
map.put("2", "b");
map.put(3, "c");
map.put(4L, "d");

// The verify check is split by entries once HashMap is an unordered map

adapter.write(map, writer, context);

InOrder order = inOrder(writer, context);

order.verify(writer).writeArrayStart(3);
order.verify(writer).writeArrayStart(4);

InOrder first = inOrder(writer, context);

Expand All @@ -50,6 +52,11 @@ public void write() throws Exception
third.verify(writer).writeKey(3);
third.verify(context).write("c", writer);

InOrder fourth = inOrder(writer, context);

fourth.verify(writer).writeKey(4L);
fourth.verify(context).write("d", writer);

order.verify(writer).writeArrayEnd();

order.verifyNoMoreInteractions();
Expand Down