JsonOptimizer

Struct JsonOptimizer 

Source
pub struct JsonOptimizer;
Expand description

JSON optimization utilities for reducing payload sizes and improving performance.

This struct provides methods for optimizing JSON payloads by removing unnecessary fields, compressing data, and minimizing payload sizes while maintaining functionality.

Implementations§

Source§

impl JsonOptimizer

Source

pub fn optimize_request_payload(json: &Value) -> Value

Optimize JSON payload by removing unnecessary fields and minimizing size.

This method recursively processes JSON objects and arrays to remove null values, empty arrays, and optimize the structure for smaller payload sizes while maintaining data integrity.

§Arguments
  • json - The JSON value to optimize
§Returns

Returns an optimized JSON value with reduced size.

§Examples
use ultrafast_gateway::json_optimization::JsonOptimizer;
use serde_json::json;

let original = json!({
    "model": "gpt-4",
    "messages": [{"role": "user", "content": "Hello"}],
    "unnecessary_field": null,
    "empty_array": []
});

let optimized = JsonOptimizer::optimize_request_payload(&original);
// Result: {"model": "gpt-4", "messages": [{"role": "user", "content": "Hello"}]}
Source

pub fn create_minimal_response(data: &Value) -> Value

Create a minimal JSON response with only essential fields.

This method creates a standardized minimal response structure that contains only the essential data field, reducing response size and improving parsing performance.

§Arguments
  • data - The data to include in the response
§Returns

Returns a minimal JSON response structure.

§Examples
use ultrafast_gateway::json_optimization::JsonOptimizer;
use serde_json::json;

let data = json!({"result": "success"});
let response = JsonOptimizer::create_minimal_response(&data);
// Result: {"data": {"result": "success"}}
Source

pub fn optimize_chat_request(request: &Value) -> Value

Optimize chat completion request by removing unnecessary fields.

This method specifically optimizes chat completion requests by keeping only the essential fields required for the API call, removing any unnecessary or null fields to reduce payload size.

§Arguments
  • request - The chat completion request to optimize
§Returns

Returns an optimized chat completion request with only essential fields.

§Examples
use ultrafast_gateway::json_optimization::JsonOptimizer;
use serde_json::json;

let request = json!({
    "model": "gpt-4",
    "messages": [{"role": "user", "content": "Hello"}],
    "temperature": 0.7,
    "max_tokens": 100,
    "unnecessary_field": null,
    "extra_config": {}
});

let optimized = JsonOptimizer::optimize_chat_request(&request);
// Keeps only: model, messages, temperature, max_tokens, etc.
Source

pub fn optimize_embedding_request(request: &Value) -> Value

Optimize embedding request by removing unnecessary fields.

This method specifically optimizes embedding requests by keeping only the essential fields required for the embedding API call, removing any unnecessary or null fields to reduce payload size.

§Arguments
  • request - The embedding request to optimize
§Returns

Returns an optimized embedding request with only essential fields.

§Examples
use ultrafast_gateway::json_optimization::JsonOptimizer;
use serde_json::json;

let request = json!({
    "model": "text-embedding-ada-002",
    "input": "Hello world",
    "encoding_format": "float",
    "dimensions": 1536,
    "unnecessary_field": null
});

let optimized = JsonOptimizer::optimize_embedding_request(&request);
// Keeps only: model, input, encoding_format, dimensions
Source

pub fn compress_json(json: &Value) -> Value

Compress JSON by using shorter field names where possible

Source

pub fn decompress_json(json: &Value) -> Value

Decompress JSON by restoring original field names

Source

pub fn calculate_payload_size(json: &Value) -> usize

Calculate JSON payload size in bytes

Source

pub fn get_size_reduction(original: &Value, optimized: &Value) -> f64

Get payload size reduction percentage

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,