Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse encoded cbor data item #12

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
19 changes: 19 additions & 0 deletions src/main/java/com/authlete/cbor/CBORByteArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -313,6 +314,24 @@ private List<? extends CBORItem> prepareDecodedContent()
}
}

@Override
protected Object parse(Number tagNumber) {
if (!isEncodedCborDataItem(tagNumber)) {
return getValue();
}
List<? extends CBORItem> items = prepareDecodedContent();
if (items != null) {
if (items.size() > 1) {
List<Object> list = new ArrayList<>();
items.stream().map(CBORItem::parse).forEachOrdered(list::add);
return list;
} else {
return items.get(0).parse();
}
} else {
return getValue();
}
}

private String prettifyString(String comment)
{
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/com/authlete/cbor/CBORItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,22 @@ public void setComment(String comment)
*/
public abstract Object parse();

/**
* Convert this {@link CBORItem} instance into an instance of a common Java class. The default
* implementation of this method calls the {@code parse()} method.
*
* @param tagNumber
* The tag number of the tag wrapping this CBOR item. If this CBOR
* item is not wrapped, {@code null} is passed.
*
* @return
* An instance of a common Java class that represents this CBOR data item.
*/
protected Object parse(Number tagNumber)
{
return parse();
}


/**
* Write the CBOR representation of this instance into the output stream.
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/authlete/cbor/CBORTaggedItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,6 @@ public void encode(OutputStream outputStream) throws IOException
@Override
public Object parse()
{
return tagContent.parse();
return tagContent.parse(tagNumber);
}
}
25 changes: 25 additions & 0 deletions src/test/java/com/authlete/cbor/CBORParsingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,31 @@ public void test_byte_array() throws IOException
assertArrayEquals(expected, (byte[])object);
}

@Test
public void test_byte_array_cbor() throws IOException
{
// === byte[] ===
//
// CBOR data item representing tagged CBOR data
// encoded d818456449455446.
//
// major = 24 ; byte string
//
byte[] input = {
(byte)0xd8, (byte)0x18, (byte)0x45, (byte)0x64,
(byte)0x49, (byte)0x45, (byte)0x54, (byte)0x46
};

// Expected result.
String expected ="IETF";

// Parse the CBOR data item.
Object object = new CBORParser(input).next();

// The object should be a String instance.
assertSame(String.class, object.getClass());
assertEquals(expected, (String)object);
}

@Test
public void test_string() throws IOException
Expand Down