Autonomy Software C++ 24.5.1
Welcome to the Autonomy Software repository of the Mars Rover Design Team (MRDT) at Missouri University of Science and Technology (Missouri S&T)! API reference contains the source code and other resources for the development of the autonomy software for our Mars rover. The Autonomy Software project aims to compete in the University Rover Challenge (URC) by demonstrating advanced autonomous capabilities and robust navigation algorithms.
Loading...
Searching...
No Matches
duckdb::JSONFormatter Struct Reference
Inheritance diagram for duckdb::JSONFormatter:
Collaboration diagram for duckdb::JSONFormatter:

Classes

struct  FormatState
 

Static Public Member Functions

static void FormatValue (BoxRenderValue &render_value, idx_t max_rows, idx_t max_width)
 

Protected Types

enum class  InlineMode { STANDARD , INLINED_SINGLE_LINE , INLINED_MULTI_LINE }
 
- Protected Types inherited from duckdb::JSONParser
enum class  JSONState { REGULAR , IN_QUOTE , ESCAPE }
 

Protected Member Functions

void HandleNull () override
 
void HandleBracketOpen (char bracket) override
 
void HandleBracketClose (char bracket) override
 
void HandleQuoteStart (char quote) override
 
void HandleQuoteEnd (char quote) override
 
void HandleComma (char comma) override
 
void HandleColon () override
 
void HandleCharacter (char c) override
 
void HandleEscapeStart (char c) override
 
void AddLiteralCharacter (char c)
 
bool LiteralFits (FormatState &format_state, idx_t render_width)
 
bool LiteralFits (FormatState &format_state, const string &text)
 
void AddLiteral (FormatState &format_state, const string &text, bool skip_adding_if_does_not_fit=false)
 
void AddSpace (FormatState &format_state)
 
void AddNewline (FormatState &format_state)
 
void FormatComponent (FormatState &format_state, JSONComponent &component, InlineMode inline_mode)
 
JSONFormattingResult TryFormat (JSONFormattingMode mode, BoxRenderValue &render_value, idx_t max_rows, idx_t max_width, idx_t indentation_size=2)
 
- Protected Member Functions inherited from duckdb::JSONParser
virtual void Finish ()
 
bool SeparatorIsMatching (Separator &sep, char closing_sep)
 
idx_t Depth () const
 

Protected Attributes

vector< JSONComponentcomponents
 
- Protected Attributes inherited from duckdb::JSONParser
JSONState state = JSONState::REGULAR
 
vector< Separatorseparators
 
idx_t pos = 0
 
bool success = true
 

Additional Inherited Members

- Public Member Functions inherited from duckdb::JSONParser
bool Process (const string &value)
 

Member Enumeration Documentation

◆ InlineMode

enum class duckdb::JSONFormatter::InlineMode
strongprotected
48609{ STANDARD, INLINED_SINGLE_LINE, INLINED_MULTI_LINE };

Constructor & Destructor Documentation

◆ JSONFormatter()

duckdb::JSONFormatter::JSONFormatter ( )
inlineexplicit
48469 {
48470 }

Member Function Documentation

◆ FormatValue()

static void duckdb::JSONFormatter::FormatValue ( BoxRenderValue render_value,
idx_t  max_rows,
idx_t  max_width 
)
inlinestatic
48472 {
48473 // process the components
48474 JSONFormatter formatter;
48475 formatter.Process(render_value.text);
48476
48477 idx_t indentation_size = 2;
48478
48479 auto result =
48480 formatter.TryFormat(JSONFormattingMode::STANDARD, render_value, max_rows, max_width, indentation_size);
48481 if (result == JSONFormattingResult::SUCCESS) {
48482 return;
48483 }
48484 // if we exceeded the max row count - try in compact mode
48485 JSONFormattingMode mode;
48486 if (result == JSONFormattingResult::TOO_WIDE) {
48487 // reduce indentation size if the result was too wide
48488 mode = JSONFormattingMode::COMPACT_HORIZONTAL;
48489 indentation_size = 1;
48490 } else {
48491 mode = JSONFormattingMode::COMPACT_VERTICAL;
48492 }
48493 result = formatter.TryFormat(mode, render_value, max_rows, max_width, indentation_size);
48494 if (result == JSONFormattingResult::SUCCESS) {
48495 return;
48496 }
48497 }
Definition duckdb.cpp:15378

◆ HandleNull()

void duckdb::JSONFormatter::HandleNull ( )
inlineoverrideprotectedvirtual

Reimplemented from duckdb::JSONParser.

48500 {
48501 components.emplace_back(JSONComponentType::NULL_VALUE, "null");
48502 }

◆ HandleBracketOpen()

void duckdb::JSONFormatter::HandleBracketOpen ( char  bracket)
inlineoverrideprotectedvirtual

Reimplemented from duckdb::JSONParser.

48504 {
48505 components.emplace_back(JSONComponentType::BRACKET_OPEN, string(1, bracket));
48506 }

◆ HandleBracketClose()

void duckdb::JSONFormatter::HandleBracketClose ( char  bracket)
inlineoverrideprotectedvirtual

Reimplemented from duckdb::JSONParser.

48508 {
48509 components.emplace_back(JSONComponentType::BRACKET_CLOSE, string(1, bracket));
48510 }

◆ HandleQuoteStart()

void duckdb::JSONFormatter::HandleQuoteStart ( char  quote)
inlineoverrideprotectedvirtual

Reimplemented from duckdb::JSONParser.

48512 {
48513 AddLiteralCharacter(quote);
48514 }

◆ HandleQuoteEnd()

void duckdb::JSONFormatter::HandleQuoteEnd ( char  quote)
inlineoverrideprotectedvirtual

Reimplemented from duckdb::JSONParser.

48516 {
48517 AddLiteralCharacter(quote);
48518 }

◆ HandleComma()

void duckdb::JSONFormatter::HandleComma ( char  comma)
inlineoverrideprotectedvirtual

Reimplemented from duckdb::JSONParser.

48520 {
48521 components.emplace_back(JSONComponentType::COMMA, ",");
48522 }

◆ HandleColon()

void duckdb::JSONFormatter::HandleColon ( )
inlineoverrideprotectedvirtual

Reimplemented from duckdb::JSONParser.

48524 {
48525 components.emplace_back(JSONComponentType::COLON, ":");
48526 }

◆ HandleCharacter()

void duckdb::JSONFormatter::HandleCharacter ( char  c)
inlineoverrideprotectedvirtual

Reimplemented from duckdb::JSONParser.

48528 {
48529 AddLiteralCharacter(c);
48530 }

◆ HandleEscapeStart()

void duckdb::JSONFormatter::HandleEscapeStart ( char  c)
inlineoverrideprotectedvirtual

Reimplemented from duckdb::JSONParser.

48532 {
48533 AddLiteralCharacter(c);
48534 }

◆ AddLiteralCharacter()

void duckdb::JSONFormatter::AddLiteralCharacter ( char  c)
inlineprotected
48536 {
48537 if (components.empty() || components.back().type != JSONComponentType::LITERAL) {
48538 components.emplace_back(JSONComponentType::LITERAL, "");
48539 }
48540 components.back().text += c;
48541 }

◆ LiteralFits() [1/2]

bool duckdb::JSONFormatter::LiteralFits ( FormatState format_state,
idx_t  render_width 
)
inlineprotected
48556 {
48557 auto &line_length = format_state.line_length;
48558 if (line_length + render_width > format_state.max_width) {
48559 return false;
48560 }
48561 return true;
48562 }

◆ LiteralFits() [2/2]

bool duckdb::JSONFormatter::LiteralFits ( FormatState format_state,
const string &  text 
)
inlineprotected
48564 {
48565 idx_t render_width = Utf8Proc::RenderWidth(text);
48566 return LiteralFits(format_state, render_width);
48567 }
static size_t RenderWidth(const char *s, size_t len, size_t pos)
Returns the render width of a single character in a string.

◆ AddLiteral()

void duckdb::JSONFormatter::AddLiteral ( FormatState format_state,
const string &  text,
bool  skip_adding_if_does_not_fit = false 
)
inlineprotected
48569 {
48570 auto &result = format_state.result;
48571 auto &line_length = format_state.line_length;
48572 idx_t render_width = Utf8Proc::RenderWidth(text);
48573 if (!LiteralFits(format_state, render_width)) {
48574 if (skip_adding_if_does_not_fit) {
48575 return;
48576 }
48577 AddNewline(format_state);
48578 if (format_state.format_result != JSONFormattingResult::SUCCESS) {
48579 return;
48580 }
48581 }
48582 result += text;
48583 line_length += render_width;
48584 if (line_length > format_state.max_width) {
48585 format_state.format_result = JSONFormattingResult::TOO_WIDE;
48586 }
48587 }

◆ AddSpace()

void duckdb::JSONFormatter::AddSpace ( FormatState format_state)
inlineprotected
48588 {
48589 AddLiteral(format_state, " ", true);
48590 }

◆ AddNewline()

void duckdb::JSONFormatter::AddNewline ( FormatState format_state)
inlineprotected
48591 {
48592 auto &result = format_state.result;
48593 auto &depth = format_state.depth;
48594 auto &row_count = format_state.row_count;
48595 auto &line_length = format_state.line_length;
48596 result += '\n';
48597 result += string(depth, ' ');
48598 row_count++;
48599 if (row_count > format_state.max_rows) {
48600 format_state.format_result = JSONFormattingResult::TOO_MANY_ROWS;
48601 return;
48602 }
48603 line_length = depth;
48604 if (line_length > format_state.max_width) {
48605 format_state.format_result = JSONFormattingResult::TOO_WIDE;
48606 }
48607 }
double depth(InputArray R, InputArray t, InputArray X)

◆ FormatComponent()

void duckdb::JSONFormatter::FormatComponent ( FormatState format_state,
JSONComponent component,
InlineMode  inline_mode 
)
inlineprotected
48611 {
48612 auto &depth = format_state.depth;
48613 auto &line_length = format_state.line_length;
48614 auto &max_width = format_state.max_width;
48615 auto &c = format_state.component_idx;
48616 switch (component.type) {
48617 case JSONComponentType::BRACKET_OPEN: {
48618 depth += component.text == "{" ? format_state.indentation_size : 1;
48619 AddLiteral(format_state, component.text);
48620 if (inline_mode == InlineMode::STANDARD) {
48621 // not inlined
48622 // look forward until the corresponding bracket open - can we inline and not exceed the column width?
48623 idx_t peek_depth = 0;
48624 idx_t render_size = line_length;
48625 idx_t peek_idx;
48626 InlineMode inline_child_mode = InlineMode::STANDARD;
48627 for (peek_idx = c + 1; peek_idx < components.size() && render_size <= max_width; peek_idx++) {
48628 auto &peek_component = components[peek_idx];
48629 if (peek_component.type == JSONComponentType::BRACKET_OPEN) {
48630 peek_depth++;
48631 } else if (peek_component.type == JSONComponentType::BRACKET_CLOSE) {
48632 if (peek_depth == 0) {
48633 // close!
48634 if (render_size + 1 < max_width) {
48635 // fits within a single line - inline on a single line
48636 inline_child_mode = InlineMode::INLINED_SINGLE_LINE;
48637 }
48638 break;
48639 }
48640 peek_depth--;
48641 }
48642 render_size += Utf8Proc::RenderWidth(peek_component.text);
48643 if (peek_component.type == JSONComponentType::COMMA ||
48644 peek_component.type == JSONComponentType::COLON) {
48645 render_size++;
48646 }
48647 }
48648 if (component.text == "[") {
48649 // for arrays - we always inline them UNLESS there are complex objects INSIDE of the bracket
48650 // scan forward until the end of the array to figure out if this is true or not
48651 for (peek_idx = c + 1; peek_idx < components.size(); peek_idx++) {
48652 auto &peek_component = components[peek_idx];
48653 peek_depth = 0;
48654 if (peek_component.type == JSONComponentType::BRACKET_OPEN) {
48655 if (peek_component.text == "{") {
48656 // nested structure within the array
48657 break;
48658 }
48659 peek_depth++;
48660 }
48661 if (peek_component.type == JSONComponentType::BRACKET_CLOSE) {
48662 if (peek_depth == 0) {
48663 inline_child_mode = InlineMode::INLINED_MULTI_LINE;
48664 break;
48665 }
48666 peek_depth--;
48667 }
48668 }
48669 }
48670 if (inline_child_mode != InlineMode::STANDARD) {
48671 // we can inline! do it
48672 for (idx_t inline_idx = c + 1; inline_idx <= peek_idx; inline_idx++) {
48673 auto &inline_component = components[inline_idx];
48674 if (inline_child_mode == InlineMode::INLINED_MULTI_LINE && inline_idx + 1 <= peek_idx) {
48675 auto &next_component = components[inline_idx + 1];
48676 if (next_component.type == JSONComponentType::COMMA ||
48677 next_component.type == JSONComponentType::BRACKET_CLOSE) {
48678 if (!LiteralFits(format_state, inline_component.text + next_component.text)) {
48679 AddNewline(format_state);
48680 }
48681 }
48682 }
48683 FormatComponent(format_state, inline_component, inline_child_mode);
48684 }
48685 c = peek_idx;
48686 return;
48687 }
48688 if (format_state.mode == JSONFormattingMode::COMPACT_VERTICAL) {
48689 // we can't inline - but is the next token a bracket open?
48690 if (c + 1 < components.size() && components[c + 1].type == JSONComponentType::BRACKET_OPEN) {
48691 // it is! that bracket open will add a newline - we don't need to do it here
48692 return;
48693 }
48694 }
48695 AddNewline(format_state);
48696 }
48697 break;
48698 }
48699 case JSONComponentType::BRACKET_CLOSE: {
48700 idx_t depth_diff = component.text == "}" ? format_state.indentation_size : 1;
48701 if (depth < depth_diff) {
48702 // shouldn't happen - but guard against underflows
48703 depth = 0;
48704 } else {
48705 depth -= depth_diff;
48706 }
48707 if (inline_mode == InlineMode::STANDARD) {
48708 AddNewline(format_state);
48709 }
48710 AddLiteral(format_state, component.text);
48711 break;
48712 }
48713 case JSONComponentType::COMMA:
48714 case JSONComponentType::COLON:
48715 AddLiteral(format_state, component.text);
48716 bool always_inline;
48717 if (format_state.mode == JSONFormattingMode::COMPACT_HORIZONTAL) {
48718 // if we are trying to compact horizontally - don't inline colons unless it fits
48719 always_inline = false;
48720 } else {
48721 // in normal processing we always inline colons
48722 always_inline = component.type == JSONComponentType::COLON;
48723 }
48724 if (inline_mode != InlineMode::STANDARD || always_inline) {
48725 AddSpace(format_state);
48726 } else {
48727 if (format_state.mode != JSONFormattingMode::STANDARD) {
48728 // if we are not inlining in compact mode, try to inline until the next comma
48729 idx_t peek_depth = 0;
48730 idx_t render_size = line_length + 1;
48731 idx_t peek_idx;
48732 bool inline_comma = false;
48733 for (peek_idx = c + 1; peek_idx < components.size() && render_size <= max_width; peek_idx++) {
48734 auto &peek_component = components[peek_idx];
48735 if (peek_component.type == JSONComponentType::BRACKET_OPEN) {
48736 peek_depth++;
48737 } else if (peek_component.type == JSONComponentType::BRACKET_CLOSE) {
48738 if (peek_depth == 0) {
48739 inline_comma = render_size + 1 < max_width;
48740 break;
48741 }
48742 peek_depth--;
48743 }
48744 if (peek_depth == 0 && peek_component.type == JSONComponentType::COMMA) {
48745 // found the next comma - inline!
48746 inline_comma = render_size + 2 <= max_width;
48747 break;
48748 }
48749 render_size += Utf8Proc::RenderWidth(peek_component.text);
48750 if (peek_component.type == JSONComponentType::COMMA ||
48751 peek_component.type == JSONComponentType::COLON) {
48752 render_size++;
48753 }
48754 }
48755 if (inline_comma) {
48756 // we can inline until the next comma! do it
48757 AddSpace(format_state);
48758 for (idx_t inline_idx = c + 1; inline_idx < peek_idx; inline_idx++) {
48759 auto &inline_component = components[inline_idx];
48760 FormatComponent(format_state, inline_component, InlineMode::INLINED_SINGLE_LINE);
48761 }
48762 c = peek_idx - 1;
48763 return;
48764 }
48765 }
48766 AddNewline(format_state);
48767 }
48768 break;
48769 case JSONComponentType::NULL_VALUE:
48770 case JSONComponentType::LITERAL:
48771 AddLiteral(format_state, component.text);
48772 break;
48773 default:
48774 throw InternalException("Unsupported JSON component type");
48775 }
48776 }

◆ TryFormat()

JSONFormattingResult duckdb::JSONFormatter::TryFormat ( JSONFormattingMode  mode,
BoxRenderValue render_value,
idx_t  max_rows,
idx_t  max_width,
idx_t  indentation_size = 2 
)
inlineprotected
48779 {
48780 FormatState format_state;
48781 format_state.mode = mode;
48782 format_state.max_rows = max_rows;
48783 format_state.max_width = max_width;
48784 format_state.indentation_size = indentation_size;
48785 for (format_state.component_idx = 0; format_state.component_idx < components.size() &&
48786 format_state.format_result == JSONFormattingResult::SUCCESS;
48787 format_state.component_idx++) {
48788 auto &component = components[format_state.component_idx];
48789 FormatComponent(format_state, component, InlineMode::STANDARD);
48790 }
48791
48792 if (format_state.format_result != JSONFormattingResult::SUCCESS) {
48793 return format_state.format_result;
48794 }
48795 render_value.text = format_state.result;
48796 return JSONFormattingResult::SUCCESS;
48797 }

The documentation for this struct was generated from the following file: