From 6bf5eecd3ffda1da917077b2e9e837536b7df9fb Mon Sep 17 00:00:00 2001 From: mtabacman Date: Fri, 24 May 2024 13:21:27 -0300 Subject: [PATCH] Changed JSON extension example to prevent incompatibilities with GemStone protocol --- .../PetOrdersRESTfulControllerTest.class.st | 11 ++++---- .../PetOrdersRESTfulController.class.st | 26 +++++++------------ ...ss.st => InstanceCreationMapping.class.st} | 12 ++++----- .../NeoJSONReader.extension.st | 10 +++---- 4 files changed, 26 insertions(+), 33 deletions(-) rename source/Stargate-NeoJSON-Extensions/{ValidCompleteInstanceMapping.class.st => InstanceCreationMapping.class.st} (82%) diff --git a/source/Stargate-Examples-Tests/PetOrdersRESTfulControllerTest.class.st b/source/Stargate-Examples-Tests/PetOrdersRESTfulControllerTest.class.st index d2e8331..fc60e29 100644 --- a/source/Stargate-Examples-Tests/PetOrdersRESTfulControllerTest.class.st +++ b/source/Stargate-Examples-Tests/PetOrdersRESTfulControllerTest.class.st @@ -53,8 +53,7 @@ PetOrdersRESTfulControllerTest >> createOverlyComplexOrder [ ^ resourceController createOrderBasedOn: ( self requestToPOSTAsOverlyComplexOrder: - ( '{"date":{"date":{"year":2018,"month":10,"day":24,"offset":0},"time":"18:05:46.418Z"},"pet":"<1p>"}' - expandMacrosWith: self petUrl ) ) + '{"date":"2018-10-24T18:05:46.418Z","pet":{"alternativeName":"Fido","itsType":"Dog","theStatus":"happy"}}' ) within: self newHttpRequestContext ] @@ -531,10 +530,10 @@ PetOrdersRESTfulControllerTest >> testOverlyComplexOrderCreation [ assert: orderRepository count equals: 1. order := orderRepository findAll first. self - assert: order pet equals: self petUrl; - assert: order date equals: ( DateAndTime - date: ( Date readFrom: '2018-10-24' pattern: 'yyyy-mm-dd' ) - time: ( Time fromString: '18:05:46.418' ) ) + assert: order pet name equals: 'Fido'; + assert: order pet type equals: 'Dog'; + assert: order pet status equals: 'HAPPY'; + assert: order date equals: '2018-10-24T18:05:46.418Z' ] { #category : 'tests' } diff --git a/source/Stargate-Examples/PetOrdersRESTfulController.class.st b/source/Stargate-Examples/PetOrdersRESTfulController.class.st index 6158ec8..30f8e25 100644 --- a/source/Stargate-Examples/PetOrdersRESTfulController.class.st +++ b/source/Stargate-Examples/PetOrdersRESTfulController.class.st @@ -128,8 +128,7 @@ PetOrdersRESTfulController >> configureOrderDecodingOn: reader [ PetOrdersRESTfulController >> configureOrderEncodingOn: writer within: requestContext [ writer - for: DateAndTime - customDo: [ :mapping | mapping encoder: [ :dateAndTime | dateAndTime printString ] ]; + for: Pet do: [ :mapping | mapping mapInstVars ]; for: ZnUrl customDo: [ :mapping | mapping encoder: [ :url | url printString ] ]; for: #Order do: [ :mapping | mapping @@ -143,25 +142,20 @@ PetOrdersRESTfulController >> configureOrderEncodingOn: writer within: requestCo { #category : 'private' } PetOrdersRESTfulController >> configureOverlyComplexOrderDecodingOn: reader [ - reader for: #Url customDo: [ :mapping | mapping decoder: [ :string | string asUrl ] ]. - reader for: #Time customDo: [ :mapping | mapping decoder: [ :string | string asTime ] ]. - reader for: Date createInstanceUsing: [ :mapping | + reader for: #Status customDo: [ :mapping | mapping decoder: [ :string | string asUppercase ] ]. + reader for: Pet createInstanceUsing: [ :mapping | mapping - mapProperty: #year; - mapProperty: #month; - mapProperty: #day. - mapping mapCreationSending: #newDay:month:year: withArguments: { #day. #month. #year } - ]. - reader for: DateAndTime createInstanceUsing: [ :mapping | + mapProperty: #alternativeName; + mapProperty: #itsType; + mapProperty: #theStatus as: #Status. mapping - mapProperty: #date as: Date; - mapProperty: #time as: #Time. - mapping mapCreationSending: #date:time: withArguments: { #date. #time } + mapCreationSending: #named:ofType:withStatus: + withArguments: { #alternativeName. #itsType. #theStatus } ]. reader for: PetOrder createInstanceUsing: [ :mapping | mapping - mapProperty: #date as: DateAndTime; - mapProperty: #pet as: #Url. + mapProperty: #date; + mapProperty: #pet as: Pet. mapping mapCreationSending: #for:on: withArguments: { #pet. #date } ]. diff --git a/source/Stargate-NeoJSON-Extensions/ValidCompleteInstanceMapping.class.st b/source/Stargate-NeoJSON-Extensions/InstanceCreationMapping.class.st similarity index 82% rename from source/Stargate-NeoJSON-Extensions/ValidCompleteInstanceMapping.class.st rename to source/Stargate-NeoJSON-Extensions/InstanceCreationMapping.class.st index c9f77d7..48ffac3 100644 --- a/source/Stargate-NeoJSON-Extensions/ValidCompleteInstanceMapping.class.st +++ b/source/Stargate-NeoJSON-Extensions/InstanceCreationMapping.class.st @@ -4,7 +4,7 @@ I will fail on reading properties of an object if some of the mapped properties " Class { - #name : 'ValidCompleteInstanceMapping', + #name : 'InstanceCreationMapping', #superclass : 'NeoJSONObjectMapping', #instVars : [ 'instanceCreationSelector', @@ -15,7 +15,7 @@ Class { } { #category : 'private' } -ValidCompleteInstanceMapping >> errorDescriptionForMissing: propertyNames [ +InstanceCreationMapping >> errorDescriptionForMissing: propertyNames [ ^ String streamContents: [ :stream | stream @@ -38,14 +38,14 @@ ValidCompleteInstanceMapping >> errorDescriptionForMissing: propertyNames [ ] { #category : 'mapping' } -ValidCompleteInstanceMapping >> mapCreationSending: anInstanceCreationSelector withArguments: anArgumentCollection [ +InstanceCreationMapping >> mapCreationSending: anInstanceCreationSelector withArguments: anArgumentCollection [ instanceCreationSelector := anInstanceCreationSelector. argumentNames := anArgumentCollection ] { #category : 'mapping' } -ValidCompleteInstanceMapping >> mapProperty: aKey [ +InstanceCreationMapping >> mapProperty: aKey [ ^ self mapProperty: aKey @@ -54,13 +54,13 @@ ValidCompleteInstanceMapping >> mapProperty: aKey [ ] { #category : 'mapping' } -ValidCompleteInstanceMapping >> mapProperty: aKey as: aValueSchema [ +InstanceCreationMapping >> mapProperty: aKey as: aValueSchema [ ( self mapProperty: aKey ) valueSchema: aValueSchema ] { #category : 'parsing' } -ValidCompleteInstanceMapping >> readFrom: jsonReader [ +InstanceCreationMapping >> readFrom: jsonReader [ | argumentByName arguments missingArguments | diff --git a/source/Stargate-NeoJSON-Extensions/NeoJSONReader.extension.st b/source/Stargate-NeoJSON-Extensions/NeoJSONReader.extension.st index 53e888d..5a54227 100644 --- a/source/Stargate-NeoJSON-Extensions/NeoJSONReader.extension.st +++ b/source/Stargate-NeoJSON-Extensions/NeoJSONReader.extension.st @@ -5,7 +5,7 @@ NeoJSONReader >> for: schemaName createInstanceUsing: block [ | mapping | - mapping := self validCompleteInstanceMappingFor: schemaName. + mapping := self instanceCreationMappingFor: schemaName. block value: mapping. ^ mapping ] @@ -21,20 +21,20 @@ NeoJSONReader >> for: schemaName strictDo: block [ ] { #category : '*Stargate-NeoJSON-Extensions' } -NeoJSONReader >> strictMappingFor: smalltalkClass [ +NeoJSONReader >> instanceCreationMappingFor: smalltalkClass [ ^ self mappings at: smalltalkClass ifAbsentPut: [ - NeoJSONStrictObjectMapping new + InstanceCreationMapping new subjectClass: smalltalkClass; yourself ] ] { #category : '*Stargate-NeoJSON-Extensions' } -NeoJSONReader >> validCompleteInstanceMappingFor: smalltalkClass [ +NeoJSONReader >> strictMappingFor: smalltalkClass [ ^ self mappings at: smalltalkClass ifAbsentPut: [ - ValidCompleteInstanceMapping new + NeoJSONStrictObjectMapping new subjectClass: smalltalkClass; yourself ]