Skip to content

Commit

Permalink
Merge pull request #12 from sett4/t/add-number-as-string-feature
Browse files Browse the repository at this point in the history
implement Feature.WRITE_NUMBERS_AS_STRINGS
  • Loading branch information
sett4 committed Jul 28, 2019
2 parents 5a7be13 + a2f2a89 commit 14682a4
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 21 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</parent>
<groupId>com.github.sett4</groupId>
<artifactId>jackson-dataformat-xlsx-lite</artifactId>
<version>2.9.9</version>
<version>2.9.9-1</version>
<name>Jackson-dataformat-XLSX-lite</name>
<packaging>bundle</packaging>
<description>Support for writing XLSX-encoded data via Jackson
Expand Down
42 changes: 33 additions & 9 deletions src/main/java/com/github/sett4/dataformat/xlsx/XlsxGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,11 @@ public void writeNumber(int v) throws IOException {
if (!this._arraySeparator.isEmpty()) {
this._addToArray(String.valueOf(v));
} else {
this._writer.write(this._columnIndex(), v);
if (isEnabled(Feature.WRITE_NUMBERS_AS_STRINGS)) {
this._writer.write(this._columnIndex(), String.valueOf(v));
} else {
this._writer.write(this._columnIndex(), v);
}
}
}

Expand All @@ -492,7 +496,11 @@ public void writeNumber(long v) throws IOException {
if (!this._arraySeparator.isEmpty()) {
this._addToArray(String.valueOf(v));
} else {
this._writer.write(this._columnIndex(), v);
if (isEnabled(Feature.WRITE_NUMBERS_AS_STRINGS)) {
this._writer.write(this._columnIndex(), String.valueOf(v));
} else {
this._writer.write(this._columnIndex(), v);
}
}
}

Expand All @@ -508,7 +516,11 @@ public void writeNumber(BigInteger v) throws IOException {
if (!this._arraySeparator.isEmpty()) {
this._addToArray(String.valueOf(v));
} else {
this._writer.write(this._columnIndex(), v.toString());
if (isEnabled(Feature.WRITE_NUMBERS_AS_STRINGS)) {
this._writer.write(this._columnIndex(), String.valueOf(v));
} else {
this._writer.write(this._columnIndex(), v.doubleValue());
}
}
}

Expand All @@ -521,7 +533,11 @@ public void writeNumber(double v) throws IOException {
if (!this._arraySeparator.isEmpty()) {
this._addToArray(String.valueOf(v));
} else {
this._writer.write(this._columnIndex(), v);
if (isEnabled(Feature.WRITE_NUMBERS_AS_STRINGS)) {
this._writer.write(this._columnIndex(), String.valueOf(v));
} else {
this._writer.write(this._columnIndex(), v);
}
}
}

Expand All @@ -533,7 +549,11 @@ public void writeNumber(float v) throws IOException {
if (!this._arraySeparator.isEmpty()) {
this._addToArray(String.valueOf(v));
} else {
this._writer.write(this._columnIndex(), v);
if (isEnabled(Feature.WRITE_NUMBERS_AS_STRINGS)) {
this._writer.write(this._columnIndex(), String.valueOf(v));
} else {
this._writer.write(this._columnIndex(), v);
}
}
}

Expand All @@ -545,11 +565,15 @@ public void writeNumber(BigDecimal v) throws IOException {
} else {
this._verifyValueWrite("write number");
if (!this._skipValue) {
String str = this.isEnabled(com.fasterxml.jackson.core.JsonGenerator.Feature.WRITE_BIGDECIMAL_AS_PLAIN) ? v.toPlainString() : v.toString();
if (!this._arraySeparator.isEmpty()) {
this._addToArray(String.valueOf(v));
if (isEnabled(Feature.WRITE_NUMBERS_AS_STRINGS)) {
String str = this.isEnabled(com.fasterxml.jackson.core.JsonGenerator.Feature.WRITE_BIGDECIMAL_AS_PLAIN) ? v.toPlainString() : v.toString();
if (!this._arraySeparator.isEmpty()) {
this._addToArray(String.valueOf(v));
} else {
this._writer.write(this._columnIndex(), str);
}
} else {
this._writer.write(this._columnIndex(), str);
this._writer.write(this._columnIndex(), v.doubleValue());
}
}

Expand Down
17 changes: 14 additions & 3 deletions src/test/java/com/github/sett4/dataformat/xlsx/ModuleTestBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.fasterxml.jackson.core.JsonToken;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.Arrays;

public abstract class ModuleTestBase extends junit.framework.TestCase {
Expand All @@ -29,7 +30,7 @@ protected void assertToken(JsonToken expToken, JsonToken actToken) {
fail("Expected token " + expToken + ", current token " + actToken);
}
}

/*
/**********************************************************
/* Helper methods; low-level
Expand Down Expand Up @@ -100,25 +101,27 @@ public enum Gender {MALE, FEMALE}
/**
* Slightly modified sample class from Jackson tutorial ("JacksonInFiveMinutes")
*/
@JsonPropertyOrder({"firstName", "lastName", "gender", "verified", "userImage", "i"})
@JsonPropertyOrder({"firstName", "lastName", "gender", "verified", "userImage", "i", "bigDecimal"})
protected static class FiveMinuteUser {

public String firstName, lastName;
private Gender _gender;
private boolean _isVerified;
private byte[] _userImage;
private int i;
private BigDecimal bigDecimal;

public FiveMinuteUser() {
}

public FiveMinuteUser(String first, String last, boolean verified, Gender g, int i, byte[] data) {
public FiveMinuteUser(String first, String last, boolean verified, Gender g, int i, byte[] data, BigDecimal bigDecimal) {
firstName = first;
lastName = last;
_isVerified = verified;
_gender = g;
this.i = i;
_userImage = data;
this.bigDecimal = bigDecimal;
}

public boolean isVerified() {
Expand Down Expand Up @@ -177,6 +180,14 @@ public int hashCode() {
// not really good but whatever:
return firstName.hashCode();
}

public BigDecimal getBigDecimal() {
return bigDecimal;
}

public void setBigDecimal(BigDecimal bigDecimal) {
this.bigDecimal = bigDecimal;
}
}

@JsonPropertyOrder({"id", "desc"})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
package com.github.sett4.dataformat.xlsx.serialize;

import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SequenceWriter;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.dataformat.csv.CsvSchema;
import com.github.sett4.dataformat.xlsx.ModuleTestBase;
import com.github.sett4.dataformat.xlsx.XlsxGenerator;
import com.github.sett4.dataformat.xlsx.XlsxMapper;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.Test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.*;
import java.math.BigDecimal;

public class WriteSimpleTest extends ModuleTestBase {
private final ObjectMapper MAPPER = mapperForXlsx();
Expand All @@ -29,7 +26,7 @@ public XlsxMapper mapperForXlsx() {
@Test
public void testSchema() throws Exception {
FiveMinuteUser user = new FiveMinuteUser("Silu", "Seppala", false, Gender.MALE, 123,
new byte[]{1, 2, 3, 4, 5});
new byte[]{1, 2, 3, 4, 5}, BigDecimal.ZERO);
File file = File.createTempFile("jackson-xlsx-test", ".xlsx");
System.out.println(file);

Expand Down Expand Up @@ -58,14 +55,15 @@ public void testSimpleExplicit() throws Exception {
.addColumn("userImage")
.addColumn("verified")
.addColumn("i")
.addColumn("bigDecimal")
.setUseHeader(true)
.build();

// from base, default order differs:
// @JsonPropertyOrder({"firstName", "lastName", "gender" ,"verified", "userImage"})

FiveMinuteUser user = new FiveMinuteUser("Silu", "Seppala", false, Gender.MALE, 123,
new byte[]{1, 2, 3, 4, 5});
new byte[]{1, 2, 3, 4, 5}, new BigDecimal("1234567890123456789012345678901234567890"));
File file = File.createTempFile("jackson-xlsx-test", ".xlsx");
System.out.println(file);
OutputStream outputStream = new FileOutputStream(file);
Expand All @@ -81,6 +79,29 @@ public void testSimpleExplicit() throws Exception {
assertEquals("userImage", sheet.getRow(0).getCell(3).getStringCellValue());
assertEquals("verified", sheet.getRow(0).getCell(4).getStringCellValue());
assertEquals("i", sheet.getRow(0).getCell(5).getStringCellValue());
assertEquals(new BigDecimal("1234567890123456789012345678901234567890").doubleValue(), sheet.getRow(1).getCell(6).getNumericCellValue());

}

@Test
public void testFeature_NumbersAsString() throws IOException {
XlsxMapper mapper = new XlsxMapper();
mapper.enable(XlsxGenerator.Feature.WRITE_NUMBERS_AS_STRINGS);
CsvSchema schema = mapper.schemaFor(FiveMinuteUser.class).withHeader();

FiveMinuteUser user = new FiveMinuteUser("Silu", "Seppala", false, Gender.MALE, 123,
new byte[]{1, 2, 3, 4, 5}, new BigDecimal("1234567890123456789012345678901234567890"));
File file = File.createTempFile("jackson-xlsx-test", ".xlsx");
OutputStream outputStream = new FileOutputStream(file);
mapper.writer(schema).writeValue(outputStream, user);

Workbook workbook = new XSSFWorkbook(new FileInputStream(file));
int activeSheetIndex = workbook.getActiveSheetIndex();
Sheet sheet = workbook.getSheetAt(activeSheetIndex);
assertEquals("123", sheet.getRow(1).getCell(5).getStringCellValue());
assertEquals(Cell.CELL_TYPE_STRING, sheet.getRow(1).getCell(5).getCellType());
assertEquals("1234567890123456789012345678901234567890", sheet.getRow(1).getCell(6).getStringCellValue());
assertEquals(Cell.CELL_TYPE_STRING, sheet.getRow(1).getCell(6).getCellType());
}

/*
Expand Down

0 comments on commit 14682a4

Please sign in to comment.