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
1 change: 1 addition & 0 deletions gridappsd-python-lib/gridappsd/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,6 @@
from gridappsd.gridappsd import GridAPPSD
from gridappsd.difference_builder import DifferenceBuilder
from gridappsd.app_registration import ApplicationController
import gridappsd.json_extension as json


4 changes: 2 additions & 2 deletions gridappsd-python-lib/gridappsd/app_registration.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import json
#import json
import logging
import os
try:
Expand All @@ -15,7 +15,7 @@

from . gridappsd import GridAPPSD
from . topics import REQUEST_REGISTER_APP
from . import utils
from . import utils, json_extension as json

_log = logging.getLogger(__name__)

Expand Down
4 changes: 3 additions & 1 deletion gridappsd-python-lib/gridappsd/goss.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"""
import base64
import inspect
import json
#import json
import logging
import os
import random
Expand All @@ -60,6 +60,8 @@
from stomp.exception import NotConnectedException
from time import sleep

from gridappsd import json_extension as json

_log: Logger = logging.getLogger(inspect.getmodulename(__file__))


Expand Down
3 changes: 1 addition & 2 deletions gridappsd-python-lib/gridappsd/gridappsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,13 @@
# -------------------------------------------------------------------------------

import inspect
import json
import logging
from datetime import datetime
from logging import DEBUG, INFO, WARNING, FATAL, WARN
import time

from gridappsd.goss import GOSS
from gridappsd import utils
from gridappsd import utils, json_extension as json
import gridappsd.topics as t
from gridappsd import ProcessStatusEnum
from gridappsd.houses import Houses
Expand Down
55 changes: 55 additions & 0 deletions gridappsd-python-lib/gridappsd/json_extension.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import dataclasses
import json as _json
from typing import Any, TextIO


@dataclasses.dataclass
class JsonComplex:
real: float
imag: float


def jsonDecoderExtension(obj: Any):
rv = obj
if isinstance(obj, dict):
try:
complexInstance = JsonComplex(**obj)
rv = complex(complexInstance.real, complexInstance.imag)
except:
rv = obj
return rv


class JsonEncoderExtension(_json.JSONEncoder):
def default(self, obj: Any) -> Any:
rv = None
if isinstance(obj, complex):
jsonComplexInstance = JsonComplex(
real = obj.real,
imag = obj.imag
)
rv = dataclasses.asdict(jsonComplexInstance)
elif dataclasses.is_dataclass(obj):
rv = dataclasses.asdict(obj)
else:
rv = super().default(obj)
return rv


def dump(data: Any, fo: TextIO):
rv = _json.dump(data, fo, cls=JsonEncoderExtension)


def dumps(data: Any) -> str:
rv = _json.dumps(data, cls=JsonEncoderExtension)
return rv


def load(fo: TextIO) -> Any:
rv = _json.load(fo, object_hook=jsonDecoderExtension)
return rv


def loads(data: str) -> Any:
rv = _json.loads(data, object_hook=jsonDecoderExtension)
return rv
8 changes: 5 additions & 3 deletions gridappsd-python-lib/gridappsd/register_app.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import json
import logging
import os
import sys
import time
import stomp
from gridappsd import ApplicationController, GridAPPSD, utils
from gridappsd import ApplicationController, GridAPPSD, utils, json_extension as json

def main():
loglevel = logging.INFO
Expand All @@ -25,7 +24,10 @@ def main():
_log.error("Invalid /appconfig reference...map the /appconfig to your container")
sys.exit(1)

config = json.loads(open("/appconfig").read())
config = {}
with open("/appconfig") as fo:
config = json.load(fo)
#config = json.loads(open("/appconfig").read())

if "id" not in config:
_log.error("Invalid appconfig, must have a unique id set.")
Expand Down
3 changes: 2 additions & 1 deletion gridappsd-python-lib/gridappsd/simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
import sys
import time
from copy import deepcopy
import json
#import json
import logging
from typing import Dict, List, Union

import gridappsd.topics as t
from . import json_extension as json

_log = logging.getLogger(__name__)

Expand Down
4 changes: 2 additions & 2 deletions gridappsd-python-lib/gridappsd/timeseries.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import json
from gridappsd import topics
#import json
from gridappsd import topics, json_extension as json
from typing import Optional


Expand Down
41 changes: 41 additions & 0 deletions gridappsd-python-lib/tests/test_json.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from gridappsd import json

def test_json_complex():
message = {
"key1": {
"key2": complex(3.369,4.213),
"key3": {
"key4": complex(-5.147,-6.258),
"key5": {
"real": -9.654,
"imag": 8.321
},
},
"key6": {
"real": 7.894,
"imag": -8.542,
"garbage": True
}
},
"key7": complex(7.894, -8.542)
}
serializedMessage = """{"key1": {"key2": {"real": 3.369, "imag": 4.213}, "key3": {"key4": {"real": -5.147, "imag": -6.258}, "key5": {"real": -9.654, "imag": 8.321}}, "key6": {"real": 7.894, "imag": -8.542, "garbage": true}}, "key7": {"real": 7.894, "imag": -8.542}}"""
deserializedMessage = {
"key1": {
"key2": complex(3.369,4.213),
"key3": {
"key4": complex(-5.147,-6.258),
"key5": complex(-9.654, 8.321),
},
"key6": {
"real": 7.894,
"imag": -8.542,
"garbage": True
}
},
"key7": complex(7.894, -8.542)
}
encodedStr = json.dumps(message)
assert encodedStr == serializedMessage
decodedMessage = json.loads(encodedStr)
assert decodedMessage == deserializedMessage
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "gridappsd-python-workspace"
version = "2023.9.0"
version = "2023.9.1"
description = "A GridAPPS-D Python Adapter"
authors = [
"C. Allwardt <3979063+craig8@users.noreply.github.com>",
Expand Down