Skip to content
Merged
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
39 changes: 19 additions & 20 deletions src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,11 @@ impl<'a, V: SelectValue> KeyValue<'a, V> {
fn serialize_object<O: Serialize>(
&'a self,
o: &O,
indent: &str,
newline: &str,
space: &str,
indent: Option<String>,
newline: Option<String>,
space: Option<String>,
) -> String {
let formatter =
RedisJsonFormatter::new(indent.as_bytes(), space.as_bytes(), newline.as_bytes());
let formatter = RedisJsonFormatter::new(indent, space, newline);

let mut out = serde_json::Serializer::with_formatter(Vec::new(), formatter);
o.serialize(&mut out).unwrap();
Expand All @@ -164,9 +163,9 @@ impl<'a, V: SelectValue> KeyValue<'a, V> {
fn to_json(
&'a self,
paths: &mut Vec<Path>,
indent: String,
newline: String,
space: String,
indent: Option<String>,
newline: Option<String>,
space: Option<String>,
format: Format,
) -> Result<RedisValue, Error> {
if format == Format::BSON {
Expand All @@ -191,23 +190,23 @@ impl<'a, V: SelectValue> KeyValue<'a, V> {
acc
});
Ok(self
.serialize_object(&temp_doc, &indent, &newline, &space)
.serialize_object(&temp_doc, indent, newline, space)
.into())
} else {
let path = &paths[0];
if path.is_legacy() {
Ok(self
.serialize_object(
self.get_first(paths[0].get_path())?,
&indent,
&newline,
&space,
self.get_first(&paths[0].get_path())?,
indent,
newline,
space,
)
.into())
} else {
let values = self.get_values(path.get_path())?;
Ok(self
.serialize_object(&values, &indent, &newline, &space)
.serialize_object(&values, indent, newline, space)
.into())
}
}
Expand Down Expand Up @@ -452,17 +451,17 @@ pub fn command_json_get<M: Manager>(manager: M, ctx: &Context, args: Vec<String>

let mut paths: Vec<Path> = vec![];
let mut format = Format::JSON;
let mut indent = String::new();
let mut space = String::new();
let mut newline = String::new();
let mut indent = None;
let mut space = None;
let mut newline = None;
while let Ok(arg) = args.next_string() {
match arg {
// fast way to consider arg a path by using the max length of all possible subcommands
// See #390 for the comparison of this function with/without this optimization
arg if arg.len() > JSONGET_SUBCOMMANDS_MAXSTRLEN => paths.push(Path::new(arg)),
arg if arg.eq_ignore_ascii_case(CMD_ARG_INDENT) => indent = args.next_string()?,
arg if arg.eq_ignore_ascii_case(CMD_ARG_NEWLINE) => newline = args.next_string()?,
arg if arg.eq_ignore_ascii_case(CMD_ARG_SPACE) => space = args.next_string()?,
arg if arg.eq_ignore_ascii_case(CMD_ARG_INDENT) => indent = Some(args.next_string()?),
arg if arg.eq_ignore_ascii_case(CMD_ARG_NEWLINE) => newline = Some(args.next_string()?),
arg if arg.eq_ignore_ascii_case(CMD_ARG_SPACE) => space = Some(args.next_string()?),
// Silently ignore. Compatibility with ReJSON v1.0 which has this option. See #168 TODO add support
arg if arg.eq_ignore_ascii_case(CMD_ARG_NOESCAPE) => continue,
arg if arg.eq_ignore_ascii_case(CMD_ARG_FORMAT) => {
Expand Down
47 changes: 28 additions & 19 deletions src/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,16 @@ DEALINGS IN THE SOFTWARE.
use serde_json::ser::Formatter;
use std::io;

pub struct RedisJsonFormatter<'a> {
pub struct RedisJsonFormatter {
current_indent: usize,
has_value: bool,
indent: &'a [u8],
space: &'a [u8],
newline: &'a [u8],
indent: Option<String>,
space: Option<String>,
newline: Option<String>,
}

impl<'a> RedisJsonFormatter<'a> {
pub fn new(indent: &'a [u8], space: &'a [u8], newline: &'a [u8]) -> Self {
impl RedisJsonFormatter {
pub fn new(indent: Option<String>, space: Option<String>, newline: Option<String>) -> Self {
RedisJsonFormatter {
current_indent: 0,
has_value: false,
Expand All @@ -48,19 +48,28 @@ impl<'a> RedisJsonFormatter<'a> {
}
}

fn indent<W: ?Sized>(wr: &mut W, n: usize, s: &[u8]) -> io::Result<()>
fn new_line<W: ?Sized>(&self, wr: &mut W) -> io::Result<()>
where
W: io::Write,
{
for _ in 0..n {
wr.write_all(s)?;
// Write new line if defined
if let Some(n) = self.newline.as_ref() {
wr.write_all(n.as_bytes())?;
}

// Ident the next line if defined
if let Some(s) = self.indent.as_ref() {
let bytes = s.as_bytes();
for _ in 0..self.current_indent {
wr.write_all(bytes)?;
}
}

Ok(())
}
}

impl<'a> Formatter for RedisJsonFormatter<'a> {
impl Formatter for RedisJsonFormatter {
fn begin_array<W: ?Sized>(&mut self, writer: &mut W) -> io::Result<()>
where
W: io::Write,
Expand All @@ -77,8 +86,7 @@ impl<'a> Formatter for RedisJsonFormatter<'a> {
self.current_indent -= 1;

if self.has_value {
writer.write_all(self.newline)?;
Self::indent(writer, self.current_indent, self.indent)?;
self.new_line(writer)?;
}

writer.write_all(b"]")
Expand All @@ -91,8 +99,7 @@ impl<'a> Formatter for RedisJsonFormatter<'a> {
if !first {
writer.write_all(b",")?;
}
writer.write_all(self.newline)?;
Self::indent(writer, self.current_indent, self.indent)
self.new_line(writer)
}

fn end_array_value<W: ?Sized>(&mut self, _writer: &mut W) -> io::Result<()>
Expand All @@ -119,8 +126,7 @@ impl<'a> Formatter for RedisJsonFormatter<'a> {
self.current_indent -= 1;

if self.has_value {
writer.write_all(self.newline)?;
Self::indent(writer, self.current_indent, self.indent)?;
self.new_line(writer)?;
}

writer.write_all(b"}")
Expand All @@ -133,16 +139,19 @@ impl<'a> Formatter for RedisJsonFormatter<'a> {
if !first {
writer.write_all(b",")?;
}
writer.write_all(self.newline)?;
Self::indent(writer, self.current_indent, self.indent)
self.new_line(writer)
}

fn begin_object_value<W: ?Sized>(&mut self, writer: &mut W) -> io::Result<()>
where
W: io::Write,
{
writer.write_all(b":")?;
writer.write_all(self.space)
if let Some(s) = self.space.as_ref() {
writer.write_all(s.as_bytes())
} else {
Ok(())
}
}

fn end_object_value<W: ?Sized>(&mut self, _writer: &mut W) -> io::Result<()>
Expand Down