Skip to content

Commit 04282a5

Browse files
committed
new writer for json
1 parent 183b4ad commit 04282a5

File tree

4 files changed

+67
-1
lines changed

4 files changed

+67
-1
lines changed

core/src/main/scala/org/polars/scala/polars/api/io/Writeable.scala

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import org.polars.scala.polars.internal.jni.io.write._
1010
class Writeable private[polars] (ptr: Long) {
1111
import org.polars.scala.polars.jsonMapper
1212

13-
private val _options: MutableMap[String, String] = MutableMap("writeMode" -> "errorifexists")
13+
private val _options: MutableMap[String, String] = MutableMap("write_mode" -> "errorifexists")
1414

1515
def option(key: String, value: String): Writeable = synchronized {
1616
if (Option(key).exists(_.trim.isEmpty) || Option(value).exists(_.trim.isEmpty)) {
@@ -51,4 +51,22 @@ class Writeable private[polars] (ptr: Long) {
5151
filePath = filePath,
5252
options = jsonMapper.writeValueAsString(_options)
5353
)
54+
55+
def json(filePath: String): Unit = {
56+
option("write_json_format", "json")
57+
writeJson(
58+
ptr = ptr,
59+
filePath = filePath,
60+
options = jsonMapper.writeValueAsString(_options)
61+
)
62+
}
63+
64+
def json_lines(filePath: String): Unit = {
65+
option("write_json_format", "json_lines")
66+
writeJson(
67+
ptr = ptr,
68+
filePath = filePath,
69+
options = jsonMapper.writeValueAsString(_options)
70+
)
71+
}
5472
}

core/src/main/scala/org/polars/scala/polars/internal/jni/io/write.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,10 @@ object write extends Natively {
2222
options: String
2323
): Unit
2424

25+
@native def writeJson(
26+
ptr: Long,
27+
filePath: String,
28+
options: String
29+
): Unit
30+
2531
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#![allow(non_snake_case)]
2+
3+
use jni::objects::{JObject, JString};
4+
use jni::sys::jlong;
5+
use jni::JNIEnv;
6+
use jni_fn::jni_fn;
7+
use polars::prelude::*;
8+
9+
use crate::internal_jni::io::write::{get_df_and_writer, parse_json_to_options, DynWriter};
10+
11+
#[jni_fn("org.polars.scala.polars.internal.jni.io.write$")]
12+
pub fn writeJson(
13+
mut env: JNIEnv,
14+
_object: JObject,
15+
df_ptr: jlong,
16+
filePath: JString,
17+
options: JString,
18+
) {
19+
let mut options = parse_json_to_options(&mut env, options).unwrap();
20+
21+
let json_format = options
22+
.remove("write_json_format")
23+
.and_then(|s| match s.to_lowercase().as_str() {
24+
"json" => Some(JsonFormat::Json),
25+
"json_lines" => Some(JsonFormat::JsonLines),
26+
_ => None,
27+
})
28+
.unwrap_or(JsonFormat::Json);
29+
30+
let overwrite_mode = options
31+
.remove("write_mode")
32+
.map(|s| matches!(s.to_lowercase().as_str(), "overwrite"))
33+
.unwrap_or(false);
34+
35+
let (mut dataframe, writer): (DataFrame, DynWriter) =
36+
get_df_and_writer(&mut env, df_ptr, filePath, overwrite_mode, options).unwrap();
37+
38+
let mut json_writer = JsonWriter::new(writer).with_json_format(json_format);
39+
40+
json_writer.finish(&mut dataframe).unwrap();
41+
}

native/src/internal_jni/io/write/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
pub mod avro;
22
pub mod ipc;
3+
pub mod json;
34
pub mod parquet;
45

56
use std::fs::File;

0 commit comments

Comments
 (0)