/**
 * Copyright 2015-2015 Ralph Schaer <ralphschaer@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package ch.rasc.bsoncodec.math;

import java.math.BigInteger;

import org.bson.BsonReader;
import org.bson.BsonWriter;
import org.bson.codecs.Codec;
import org.bson.codecs.DecoderContext;
import org.bson.codecs.EncoderContext;

public class BigIntegerStringCodec implements Codec<BigInteger> {

	@Override
	public Class<BigInteger> getEncoderClass() {
		return BigInteger.class;
	}

	@Override
	public void encode(BsonWriter writer, BigInteger value,
			EncoderContext encoderContext) {
		writer.writeString(value.toString());
	}

	@Override
	public BigInteger decode(BsonReader reader, DecoderContext decoderContext) {
		return new BigInteger(reader.readString());
	}

}

@@ -678,16 +678,41 @@
 Integer;
+%0Aimport java.util.Arrays;
 %0A%0Aimport
@@ -937,16 +937,403 @@
 ger%3E %7B%0A%0A
+%09private final static char%5B%5D SIGNS = %7B '-', '+', '+' %7D;%0A%0A%09/**%0A%09 * If %3E 0 left align and zero pad the String. zeroPadding specifies the total number%0A%09 * characters of the resulting String: sign + digits%0A%09 */%0A%09private final Integer zeroPadding;%0A%0A%09public BigIntegerStringCodec() %7B%0A%09%09this(null);%0A%09%7D%0A%0A%09public BigIntegerStringCodec(Integer zeroPadding) %7B%0A%09%09this.zeroPadding = zeroPadding;%0A%09%7D%0A%0A
 %09@Overri
@@ -1516,16 +1516,51 @@
 text) %7B%0A
+%09%09if (this.zeroPadding == null) %7B%0A%09
 %09%09writer
@@ -1587,24 +1587,89 @@
 oString());%0A
+%09%09%7D%0A%09%09else %7B%0A%09%09%09writer.writeString(formatBigInteger(value));%0A%09%09%7D%0A
 %09%7D%0A%0A%09@Overri
@@ -1799,10 +1799,409 @@
 ));%0A%09%7D%0A%0A
+%09private String formatBigInteger(BigInteger bd) %7B%0A%09%09char%5B%5D result = new char%5Bthis.zeroPadding%5D;%0A%09%09Arrays.fill(result, '0');%0A%09%09result%5B0%5D = SIGNS%5Bbd.signum() + 1%5D;%0A%0A%09%09String s = bd.toString();%0A%09%09if (s.startsWith(%22-%22)) %7B%0A%09%09%09s = s.substring(1);%0A%09%09%7D%0A%0A%09%09char%5B%5D source = s.toCharArray();%0A%09%09System.arraycopy(source, 0, result, result.length - source.length, source.length);%0A%0A%09%09return new String(result);%0A%09%7D%0A
 %7D%0A

Add codecs for BigInteger and BigDecimal