2021-08-06 12:59:39 +05:30
|
|
|
# SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
#
|
|
|
|
|
# The OpenSearch Contributors require contributions made to
|
|
|
|
|
# this file be licensed under the Apache-2.0 license or a
|
|
|
|
|
# compatible open source license.
|
|
|
|
|
#
|
|
|
|
|
# Modifications Copyright OpenSearch Contributors. See
|
|
|
|
|
# GitHub history for details.
|
|
|
|
|
#
|
2020-07-02 13:15:25 -05:00
|
|
|
# Licensed to Elasticsearch B.V. under one or more contributor
|
|
|
|
|
# license agreements. See the NOTICE file distributed with
|
|
|
|
|
# this work for additional information regarding copyright
|
|
|
|
|
# ownership. Elasticsearch B.V. licenses this file to you under
|
|
|
|
|
# the Apache License, Version 2.0 (the "License"); you may
|
|
|
|
|
# not use this file except in compliance with the License.
|
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
|
#
|
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
#
|
|
|
|
|
# Unless required by applicable law or agreed to in writing,
|
|
|
|
|
# software distributed under the License is distributed on an
|
|
|
|
|
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
|
|
|
# KIND, either express or implied. See the License for the
|
|
|
|
|
# specific language governing permissions and limitations
|
|
|
|
|
# under the License.
|
2020-04-23 11:22:08 -05:00
|
|
|
|
2022-10-04 00:15:18 +05:30
|
|
|
|
2023-11-06 13:08:19 -05:00
|
|
|
from typing import Any, Dict, Optional
|
|
|
|
|
|
2014-06-18 14:39:55 +02:00
|
|
|
try:
|
|
|
|
|
import simplejson as json
|
|
|
|
|
except ImportError:
|
2023-11-06 13:08:19 -05:00
|
|
|
import json # type: ignore
|
2020-03-30 13:02:18 -05:00
|
|
|
|
2015-12-09 23:15:59 -08:00
|
|
|
import uuid
|
2013-05-02 23:42:11 +02:00
|
|
|
from datetime import date, datetime
|
2013-10-22 00:26:58 +02:00
|
|
|
from decimal import Decimal
|
2013-05-02 23:42:11 +02:00
|
|
|
|
2014-02-21 16:53:56 +01:00
|
|
|
from .compat import string_types
|
2021-01-13 14:21:04 -06:00
|
|
|
from .exceptions import ImproperlyConfigured, SerializationError
|
2023-02-14 15:03:56 -08:00
|
|
|
from .helpers.utils import AttrList
|
2013-12-14 18:15:15 +01:00
|
|
|
|
2020-03-30 13:02:18 -05:00
|
|
|
INTEGER_TYPES = ()
|
|
|
|
|
FLOAT_TYPES = (Decimal,)
|
|
|
|
|
TIME_TYPES = (date, datetime)
|
|
|
|
|
|
2019-05-10 09:16:33 -06:00
|
|
|
|
2024-07-20 23:19:20 +03:00
|
|
|
class Serializer:
|
2023-11-06 13:08:19 -05:00
|
|
|
mimetype: str = ""
|
2020-08-31 11:07:04 -05:00
|
|
|
|
2023-11-06 13:08:19 -05:00
|
|
|
def loads(self, s: str) -> Any:
|
2020-09-28 09:14:03 -05:00
|
|
|
raise NotImplementedError()
|
2020-08-31 11:07:04 -05:00
|
|
|
|
2023-11-06 13:08:19 -05:00
|
|
|
def dumps(self, data: Any) -> Any:
|
2020-08-31 11:07:04 -05:00
|
|
|
raise NotImplementedError()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TextSerializer(Serializer):
|
2023-11-06 13:08:19 -05:00
|
|
|
mimetype: str = "text/plain"
|
2013-12-14 18:15:15 +01:00
|
|
|
|
2023-11-06 13:08:19 -05:00
|
|
|
def loads(self, s: str) -> Any:
|
2013-12-14 18:15:15 +01:00
|
|
|
return s
|
|
|
|
|
|
2023-11-06 13:08:19 -05:00
|
|
|
def dumps(self, data: Any) -> Any:
|
2014-02-21 16:53:56 +01:00
|
|
|
if isinstance(data, string_types):
|
2013-12-14 18:15:15 +01:00
|
|
|
return data
|
|
|
|
|
|
2024-07-20 23:19:20 +03:00
|
|
|
raise SerializationError(f"Cannot serialize {data!r} into text.")
|
2019-05-10 09:16:33 -06:00
|
|
|
|
2013-05-02 23:42:11 +02:00
|
|
|
|
2020-08-31 11:07:04 -05:00
|
|
|
class JSONSerializer(Serializer):
|
2023-11-06 13:08:19 -05:00
|
|
|
mimetype: str = "application/json"
|
2013-12-14 18:15:15 +01:00
|
|
|
|
2023-11-06 13:08:19 -05:00
|
|
|
def default(self, data: Any) -> Any:
|
2022-01-06 02:02:18 +05:30
|
|
|
if isinstance(data, TIME_TYPES):
|
|
|
|
|
# Little hack to avoid importing pandas but to not
|
|
|
|
|
# return 'NaT' string for pd.NaT as that's not a valid
|
|
|
|
|
# date.
|
|
|
|
|
formatted_data = data.isoformat()
|
|
|
|
|
if formatted_data != "NaT":
|
|
|
|
|
return formatted_data
|
|
|
|
|
|
|
|
|
|
if isinstance(data, uuid.UUID):
|
2015-12-09 23:15:59 -08:00
|
|
|
return str(data)
|
2020-03-30 13:02:18 -05:00
|
|
|
elif isinstance(data, FLOAT_TYPES):
|
|
|
|
|
return float(data)
|
|
|
|
|
elif INTEGER_TYPES and isinstance(data, INTEGER_TYPES):
|
|
|
|
|
return int(data)
|
|
|
|
|
|
|
|
|
|
# Special cases for numpy and pandas types
|
2022-01-06 02:02:18 +05:30
|
|
|
# These are expensive to import so we try them last.
|
|
|
|
|
try:
|
|
|
|
|
import numpy as np
|
|
|
|
|
|
|
|
|
|
if isinstance(
|
|
|
|
|
data,
|
|
|
|
|
(
|
|
|
|
|
np.int_,
|
|
|
|
|
np.intc,
|
|
|
|
|
np.int8,
|
|
|
|
|
np.int16,
|
|
|
|
|
np.int32,
|
|
|
|
|
np.int64,
|
|
|
|
|
np.uint8,
|
|
|
|
|
np.uint16,
|
|
|
|
|
np.uint32,
|
|
|
|
|
np.uint64,
|
|
|
|
|
),
|
|
|
|
|
):
|
|
|
|
|
return int(data)
|
|
|
|
|
elif isinstance(
|
|
|
|
|
data,
|
|
|
|
|
(
|
|
|
|
|
np.float16,
|
|
|
|
|
np.float32,
|
|
|
|
|
np.float64,
|
|
|
|
|
),
|
|
|
|
|
):
|
|
|
|
|
return float(data)
|
|
|
|
|
elif isinstance(data, np.bool_):
|
2020-03-30 13:02:18 -05:00
|
|
|
return bool(data)
|
|
|
|
|
elif isinstance(data, np.datetime64):
|
|
|
|
|
return data.item().isoformat()
|
|
|
|
|
elif isinstance(data, np.ndarray):
|
|
|
|
|
return data.tolist()
|
2022-01-06 02:02:18 +05:30
|
|
|
except ImportError:
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
import pandas as pd
|
|
|
|
|
|
2020-03-30 13:02:18 -05:00
|
|
|
if isinstance(data, (pd.Series, pd.Categorical)):
|
|
|
|
|
return data.tolist()
|
2022-01-06 02:02:18 +05:30
|
|
|
elif isinstance(data, pd.Timestamp) and data is not getattr(
|
|
|
|
|
pd, "NaT", None
|
|
|
|
|
):
|
|
|
|
|
return data.isoformat()
|
2020-06-29 10:10:05 -05:00
|
|
|
elif data is getattr(pd, "NA", None):
|
2020-03-30 13:02:18 -05:00
|
|
|
return None
|
2022-01-06 02:02:18 +05:30
|
|
|
except ImportError:
|
|
|
|
|
pass
|
2020-03-30 13:02:18 -05:00
|
|
|
|
2024-07-20 23:19:20 +03:00
|
|
|
raise TypeError(f"Unable to serialize {data!r} (type: {type(data)})")
|
2013-05-02 23:42:11 +02:00
|
|
|
|
2023-11-06 13:08:19 -05:00
|
|
|
def loads(self, s: str) -> Any:
|
2013-05-02 23:42:11 +02:00
|
|
|
try:
|
|
|
|
|
return json.loads(s)
|
2013-05-03 17:56:27 +02:00
|
|
|
except (ValueError, TypeError) as e:
|
2013-07-30 15:05:46 +02:00
|
|
|
raise SerializationError(s, e)
|
2013-05-02 23:42:11 +02:00
|
|
|
|
2023-11-06 13:08:19 -05:00
|
|
|
def dumps(self, data: Any) -> Any:
|
2013-06-20 14:07:09 +02:00
|
|
|
# don't serialize strings
|
2014-02-21 16:53:56 +01:00
|
|
|
if isinstance(data, string_types):
|
2013-12-04 15:07:56 +01:00
|
|
|
return data
|
2013-06-20 14:07:09 +02:00
|
|
|
|
2013-05-02 23:42:11 +02:00
|
|
|
try:
|
2018-01-01 17:08:38 +02:00
|
|
|
return json.dumps(
|
2019-05-10 09:16:33 -06:00
|
|
|
data, default=self.default, ensure_ascii=False, separators=(",", ":")
|
2018-01-01 17:08:38 +02:00
|
|
|
)
|
2013-05-03 17:56:27 +02:00
|
|
|
except (ValueError, TypeError) as e:
|
2013-07-30 15:05:46 +02:00
|
|
|
raise SerializationError(data, e)
|
2013-05-02 23:42:11 +02:00
|
|
|
|
2019-05-10 09:16:33 -06:00
|
|
|
|
2023-11-06 13:08:19 -05:00
|
|
|
DEFAULT_SERIALIZERS: Dict[str, Serializer] = {
|
2013-12-14 18:15:15 +01:00
|
|
|
JSONSerializer.mimetype: JSONSerializer(),
|
|
|
|
|
TextSerializer.mimetype: TextSerializer(),
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-10 09:16:33 -06:00
|
|
|
|
2024-07-20 23:19:20 +03:00
|
|
|
class Deserializer:
|
2023-11-06 13:08:19 -05:00
|
|
|
def __init__(
|
|
|
|
|
self,
|
|
|
|
|
serializers: Dict[str, Serializer],
|
|
|
|
|
default_mimetype: str = "application/json",
|
|
|
|
|
) -> None:
|
2013-12-14 18:15:15 +01:00
|
|
|
try:
|
|
|
|
|
self.default = serializers[default_mimetype]
|
|
|
|
|
except KeyError:
|
2019-05-10 09:16:33 -06:00
|
|
|
raise ImproperlyConfigured(
|
2024-07-20 23:19:20 +03:00
|
|
|
f"Cannot find default serializer ({default_mimetype})"
|
2019-05-10 09:16:33 -06:00
|
|
|
)
|
2013-12-14 18:15:15 +01:00
|
|
|
self.serializers = serializers
|
|
|
|
|
|
2023-11-06 13:08:19 -05:00
|
|
|
def loads(self, s: str, mimetype: Optional[str] = None) -> Any:
|
2013-12-14 18:15:15 +01:00
|
|
|
if not mimetype:
|
|
|
|
|
deserializer = self.default
|
|
|
|
|
else:
|
2021-08-26 18:55:25 +05:30
|
|
|
# Treat 'application/vnd.elasticsearch+json'
|
|
|
|
|
# as application/json for compatibility.
|
|
|
|
|
if mimetype == "application/vnd.elasticsearch+json":
|
|
|
|
|
mimetype = "application/json"
|
|
|
|
|
|
2021-08-16 17:20:29 +05:30
|
|
|
# split out charset
|
|
|
|
|
mimetype, _, _ = mimetype.partition(";")
|
2013-12-14 18:15:15 +01:00
|
|
|
try:
|
|
|
|
|
deserializer = self.serializers[mimetype]
|
|
|
|
|
except KeyError:
|
2019-05-10 09:16:33 -06:00
|
|
|
raise SerializationError(
|
2024-07-20 23:19:20 +03:00
|
|
|
f"Unknown mimetype, unable to deserialize: {mimetype}"
|
2019-05-10 09:16:33 -06:00
|
|
|
)
|
2013-12-14 18:15:15 +01:00
|
|
|
|
|
|
|
|
return deserializer.loads(s)
|
2023-02-14 15:03:56 -08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class AttrJSONSerializer(JSONSerializer):
|
2023-11-06 13:08:19 -05:00
|
|
|
def default(self, data: Any) -> Any:
|
2023-02-14 15:03:56 -08:00
|
|
|
if isinstance(data, AttrList):
|
|
|
|
|
return data._l_
|
|
|
|
|
if hasattr(data, "to_dict"):
|
|
|
|
|
return data.to_dict()
|
2024-07-20 23:19:20 +03:00
|
|
|
return super().default(data)
|
2023-02-14 15:03:56 -08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
serializer = AttrJSONSerializer()
|