Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Tensorflow Serving Json Tensor Test

From Leeroopedia
Knowledge Sources
Domains Testing, JSON, REST API
Last Updated 2026-02-13 00:00 GMT

Overview

Test suite validating JSON tensor conversion utilities for the REST API, covering both request parsing and response serialization.

Description

This test file provides extensive coverage of the JSON tensor conversion functions in tensorflow_serving/util/json_tensor.h. It tests FillPredictRequestFromJson for parsing JSON predict requests into TensorFlow protobuf format, and various FromJson* functions for reverse conversion. The tests use RapidJSON for JSON parsing and protobuf text format for expected value comparisons.

Key areas tested include:

  • Single unnamed tensors (row and columnar format)
  • Multiple named tensors
  • Base64 encoded bytes (scalars and lists)
  • Mixed integer/float inputs for float and double tensor types
  • Precision loss and overflow handling for float tensors
  • Non-null-terminated buffer handling
  • Deeply nested JSON (well-formed and malformed)
  • Signature name support
  • Comprehensive error cases for both row and columnar formats
  • JSON-to-tensor conversion for single and multiple tensors
  • Zero-batch tensor handling
  • Classification and regression result serialization to JSON
  • Non-finite float values (NaN, Infinity)

Usage

Run these tests to validate changes to the REST API JSON parsing/serialization, tensor format conversion, or error handling for malformed JSON inputs.

Code Reference

Source Location

  • Repository: Tensorflow_Serving
  • File: tensorflow_serving/util/json_tensor_test.cc
  • Lines: 1-1619

Test Fixture

using TensorInfoMap = ::google::protobuf::Map<string, TensorInfo>;
using TensorMap = ::google::protobuf::Map<string, TensorProto>;

std::function<absl::Status(const string&, TensorInfoMap*)> getmap(
    const TensorInfoMap& map) {
  return [&map](const string&, TensorInfoMap* m) {
    *m = map;
    return absl::OkStatus();
  };
}

// Tests use getmap() to provide tensor type info for parsing
TEST(JsontensorTest, SingleUnnamedTensor) {
  TensorInfoMap infomap;
  ASSERT_TRUE(
      TextFormat::ParseFromString("dtype: DT_INT32", &infomap["default"]));
  PredictRequest req;
  JsonPredictRequestFormat format;
  TF_EXPECT_OK(FillPredictRequestFromJson(..., getmap(infomap), &req, &format));
}

Build Target

bazel test //tensorflow_serving/util:json_tensor_test

Test Coverage

Key Test Cases

Test Name Category Description
SingleUnnamedTensor Parsing Tests parsing single unnamed tensor in row format
DeeplyNestedWellFormed Parsing Tests parsing deeply nested but valid JSON
DeeplyNestedMalformed Error Handling Tests rejection of deeply nested malformed JSON
MixedInputForFloatTensor Type Handling Tests mixed int/float input for float tensors
MixedInputForDoubleTensor Type Handling Tests mixed int/float input for double tensors
FloatTensorWithPrecisionLoss Precision Tests float precision loss handling
FloatTensorThatExceedsMaxReturnsInf Overflow Tests float overflow returns infinity
FloatTensorThatExceedsMinReturnsZero Underflow Tests float underflow returns zero
SingleUnnamedTensorWithSignature Parsing Tests parsing with signature name
TensorFromNonNullTerminatedBuffer Edge Case Tests parsing from non-null-terminated buffer
SingleUnnamedTensorBase64Scalars Base64 Tests base64 scalar byte value parsing
SingleUnnamedTensorBase64Lists Base64 Tests base64 list byte value parsing
SingleNamedTensorBase64 Base64 Tests base64 named tensor parsing
MultipleNamedTensor Parsing Tests parsing multiple named tensors
SingleUnnamedTensorColumnarFormat Format Tests columnar (inputs) format parsing
MultipleNamedTensorColumnarFormat Format Tests columnar format with multiple named tensors
SingleUnnamedTensorErrors Error Handling Tests various error cases for row format
MultipleNamedTensorErrors Error Handling Tests various error cases for named tensors
FromJsonSingleTensor Serialization Tests single tensor JSON serialization
FromJsonSingleScalarTensor Serialization Tests scalar tensor JSON serialization
FromJsonSingleBytesTensor Serialization Tests bytes tensor base64 serialization
FromJsonSingleFloatTensorSixDigitPrecision Precision Tests float serialization precision
FromJsonSingleFloatTensorNonFinite Serialization Tests NaN/Infinity serialization
FromJsonMultipleNamedTensors Serialization Tests multiple tensor JSON serialization
FromJsonSingleZeroBatchTensor Edge Case Tests zero-batch tensor handling
FromJsonMultipleZeroBatchTensors Edge Case Tests multiple zero-batch tensors
JsonFromClassificationResult Serialization Tests classification result to JSON
JsonFromRegressionResult Serialization Tests regression result to JSON
JsonFromRegressionResultWithNonFinite Serialization Tests regression with NaN/Infinity
JsonFromResultErrors Error Handling Tests error cases in result serialization
MakeJsonFromTensors.StatusOK Serialization Tests successful tensor to JSON conversion
MakeJsonFromTensors.StatusError Error Handling Tests error in tensor to JSON conversion

Usage Examples

Test Pattern

TEST(JsontensorTest, SingleUnnamedTensor) {
  TensorInfoMap infomap;
  ASSERT_TRUE(
      TextFormat::ParseFromString("dtype: DT_INT32", &infomap["default"]));

  PredictRequest req;
  JsonPredictRequestFormat format;
  TF_EXPECT_OK(FillPredictRequestFromJson(R"(
    {
      "instances": [[1,2],[3,4],[5,6]]
    })",
    getmap(infomap), &req, &format));
  auto tmap = req.inputs();
  EXPECT_EQ(tmap.size(), 1);
  EXPECT_EQ(format, JsonPredictRequestFormat::kRow);
}

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment