Skip to content

Commit

Permalink
serialization.
Browse files Browse the repository at this point in the history
  • Loading branch information
trivialfis committed Jan 21, 2025
1 parent 54876db commit d665490
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (c) 2021-2024 by Contributors
Copyright (c) 2021-2025 by Contributors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -15,11 +15,40 @@
*/
package ml.dmlc.xgboost4j.java;

import java.io.IOException;
import java.util.Iterator;
import java.util.Map;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.module.SimpleModule;

class F64NaNSerializer extends JsonSerializer<Double> {
@Override
public void serialize(Double value, JsonGenerator gen,
SerializerProvider serializers) throws IOException {
if (value.isNaN()) {
gen.writeRawValue("NaN"); // Write NaN without quotes
} else {
gen.writeNumber(value);
}
}
}

class F32NaNSerializer extends JsonSerializer<Float> {
@Override
public void serialize(Float value, JsonGenerator gen,
SerializerProvider serializers) throws IOException {
if (value.isNaN()) {
gen.writeRawValue("NaN"); // Write NaN without quotes
} else {
gen.writeNumber(value);
}
}
}

/**
* QuantileDMatrix will only be used to train
Expand Down Expand Up @@ -121,8 +150,17 @@ private String getConfig(float missing, int maxBin, int nthread) {
conf.put("max_bin", maxBin);
conf.put("nthread", nthread);
ObjectMapper mapper = new ObjectMapper();

// Handle NaN values. Jackson by default serializes NaN values into strings.
SimpleModule module = new SimpleModule();
module.addSerializer(Double.class, new F64NaNSerializer());
module.addSerializer(Float.class, new F32NaNSerializer());
mapper.registerModule(module);

try {
return mapper.writeValueAsString(conf);
String config = mapper.writeValueAsString(conf);
System.out.println(config);
return config;
} catch (JsonProcessingException e) {
throw new RuntimeException("Failed to serialize configuration", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import java.io.Serializable;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.core.JsonProcessingException;
Expand Down

0 comments on commit d665490

Please sign in to comment.